diff options
author | Chocobozzz <me@florianbigard.com> | 2018-03-28 11:00:02 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-03-28 11:00:02 +0200 |
commit | c73e83da283c6d4eb094e384d59c4f8eb221507d (patch) | |
tree | 3614954f10ffdb9048bdd10759d1a7f19925b5e9 | |
parent | 63f4b132817bfc4d5be4f89077b5a42b97b11a7c (diff) | |
download | PeerTube-c73e83da283c6d4eb094e384d59c4f8eb221507d.tar.gz PeerTube-c73e83da283c6d4eb094e384d59c4f8eb221507d.tar.zst PeerTube-c73e83da283c6d4eb094e384d59c4f8eb221507d.zip |
Truncate correctly video descriptions
-rw-r--r-- | server/helpers/core-utils.ts | 18 | ||||
-rw-r--r-- | server/helpers/custom-validators/activitypub/videos.ts | 12 | ||||
-rw-r--r-- | server/lib/activitypub/process/process.ts | 2 | ||||
-rw-r--r-- | server/models/video/video.ts | 21 |
4 files changed, 36 insertions, 17 deletions
diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts index 65f18d644..a3dfe27b5 100644 --- a/server/helpers/core-utils.ts +++ b/server/helpers/core-utils.ts | |||
@@ -12,6 +12,7 @@ import { isAbsolute, join } from 'path' | |||
12 | import * as pem from 'pem' | 12 | import * as pem from 'pem' |
13 | import * as rimraf from 'rimraf' | 13 | import * as rimraf from 'rimraf' |
14 | import { URL } from 'url' | 14 | import { URL } from 'url' |
15 | import { truncate } from 'lodash' | ||
15 | 16 | ||
16 | function sanitizeUrl (url: string) { | 17 | function sanitizeUrl (url: string) { |
17 | const urlObject = new URL(url) | 18 | const urlObject = new URL(url) |
@@ -78,6 +79,22 @@ function buildPath (path: string) { | |||
78 | return join(root(), path) | 79 | return join(root(), path) |
79 | } | 80 | } |
80 | 81 | ||
82 | // Consistent with .length, lodash truncate function is not | ||
83 | function peertubeTruncate (str: string, maxLength: number) { | ||
84 | const options = { | ||
85 | length: maxLength | ||
86 | } | ||
87 | const truncatedStr = truncate(str, options) | ||
88 | |||
89 | // The truncated string is okay, we can return it | ||
90 | if (truncatedStr.length <= maxLength) return truncatedStr | ||
91 | |||
92 | // Lodash takes into account all UTF characters, whereas String.prototype.length does not: some characters have a length of 2 | ||
93 | // We always use the .length so we need to truncate more if needed | ||
94 | options.length -= truncatedStr.length - maxLength | ||
95 | return truncate(str, options) | ||
96 | } | ||
97 | |||
81 | function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> { | 98 | function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> { |
82 | return function promisified (): Promise<A> { | 99 | return function promisified (): Promise<A> { |
83 | return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { | 100 | return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { |
@@ -145,6 +162,7 @@ export { | |||
145 | sanitizeUrl, | 162 | sanitizeUrl, |
146 | sanitizeHost, | 163 | sanitizeHost, |
147 | buildPath, | 164 | buildPath, |
165 | peertubeTruncate, | ||
148 | 166 | ||
149 | promisify0, | 167 | promisify0, |
150 | promisify1, | 168 | promisify1, |
diff --git a/server/helpers/custom-validators/activitypub/videos.ts b/server/helpers/custom-validators/activitypub/videos.ts index 10588423a..3af587a32 100644 --- a/server/helpers/custom-validators/activitypub/videos.ts +++ b/server/helpers/custom-validators/activitypub/videos.ts | |||
@@ -1,5 +1,6 @@ | |||
1 | import * as validator from 'validator' | 1 | import * as validator from 'validator' |
2 | import { ACTIVITY_PUB } from '../../../initializers' | 2 | import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers' |
3 | import { peertubeTruncate } from '../../core-utils' | ||
3 | import { exists, isBooleanValid, isDateValid, isUUIDValid } from '../misc' | 4 | import { exists, isBooleanValid, isDateValid, isUUIDValid } from '../misc' |
4 | import { | 5 | import { |
5 | isVideoAbuseReasonValid, | 6 | isVideoAbuseReasonValid, |
@@ -56,6 +57,7 @@ function isVideoTorrentObjectValid (video: any) { | |||
56 | isBooleanValid(video.commentsEnabled) && | 57 | isBooleanValid(video.commentsEnabled) && |
57 | isDateValid(video.published) && | 58 | isDateValid(video.published) && |
58 | isDateValid(video.updated) && | 59 | isDateValid(video.updated) && |
60 | setTruncatedContent(video) && | ||
59 | (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) && | 61 | (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) && |
60 | isRemoteVideoIconValid(video.icon) && | 62 | isRemoteVideoIconValid(video.icon) && |
61 | setValidRemoteVideoUrls(video) && | 63 | setValidRemoteVideoUrls(video) && |
@@ -111,6 +113,14 @@ function setValidRemoteVideoUrls (video: any) { | |||
111 | return true | 113 | return true |
112 | } | 114 | } |
113 | 115 | ||
116 | function setTruncatedContent (video: any) { | ||
117 | if (video.content) { | ||
118 | video.content = peertubeTruncate(video.content, CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max) | ||
119 | } | ||
120 | |||
121 | return true | ||
122 | } | ||
123 | |||
114 | function isRemoteVideoUrlValid (url: any) { | 124 | function isRemoteVideoUrlValid (url: any) { |
115 | return url.type === 'Link' && | 125 | return url.type === 'Link' && |
116 | ( | 126 | ( |
diff --git a/server/lib/activitypub/process/process.ts b/server/lib/activitypub/process/process.ts index 10d8ba189..da91675ce 100644 --- a/server/lib/activitypub/process/process.ts +++ b/server/lib/activitypub/process/process.ts | |||
@@ -43,7 +43,7 @@ async function processActivities (activities: Activity[], signatureActor?: Actor | |||
43 | try { | 43 | try { |
44 | await activityProcessor(activity, inboxActor) | 44 | await activityProcessor(activity, inboxActor) |
45 | } catch (err) { | 45 | } catch (err) { |
46 | logger.warn('Cannot process activity %s.', activity.type, { error: err.stack }) | 46 | logger.warn('Cannot process activity %s.', activity.type, { err }) |
47 | } | 47 | } |
48 | } | 48 | } |
49 | } | 49 | } |
diff --git a/server/models/video/video.ts b/server/models/video/video.ts index a28b5209b..7c56c65a6 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | import * as Bluebird from 'bluebird' | 1 | import * as Bluebird from 'bluebird' |
2 | import { map, maxBy, truncate } from 'lodash' | 2 | import { map, maxBy } from 'lodash' |
3 | import * as magnetUtil from 'magnet-uri' | 3 | import * as magnetUtil from 'magnet-uri' |
4 | import * as parseTorrent from 'parse-torrent' | 4 | import * as parseTorrent from 'parse-torrent' |
5 | import { join } from 'path' | 5 | import { join } from 'path' |
@@ -31,7 +31,10 @@ import { VideoTorrentObject } from '../../../shared/models/activitypub/objects' | |||
31 | import { Video, VideoDetails } from '../../../shared/models/videos' | 31 | import { Video, VideoDetails } from '../../../shared/models/videos' |
32 | import { VideoFilter } from '../../../shared/models/videos/video-query.type' | 32 | import { VideoFilter } from '../../../shared/models/videos/video-query.type' |
33 | import { activityPubCollection } from '../../helpers/activitypub' | 33 | import { activityPubCollection } from '../../helpers/activitypub' |
34 | import { createTorrentPromise, renamePromise, statPromise, unlinkPromise, writeFilePromise } from '../../helpers/core-utils' | 34 | import { |
35 | createTorrentPromise, peertubeTruncate, renamePromise, statPromise, unlinkPromise, | ||
36 | writeFilePromise | ||
37 | } from '../../helpers/core-utils' | ||
35 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | 38 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' |
36 | import { isBooleanValid } from '../../helpers/custom-validators/misc' | 39 | import { isBooleanValid } from '../../helpers/custom-validators/misc' |
37 | import { | 40 | import { |
@@ -1191,19 +1194,7 @@ export class VideoModel extends Model<VideoModel> { | |||
1191 | if (!this.description) return null | 1194 | if (!this.description) return null |
1192 | 1195 | ||
1193 | const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max | 1196 | const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max |
1194 | 1197 | return peertubeTruncate(this.description, maxLength) | |
1195 | const options = { | ||
1196 | length: maxLength | ||
1197 | } | ||
1198 | const truncatedDescription = truncate(this.description, options) | ||
1199 | |||
1200 | // The truncated string is okay, we can return it | ||
1201 | if (truncatedDescription.length <= maxLength) return truncatedDescription | ||
1202 | |||
1203 | // Lodash takes into account all UTF characters, whereas String.prototype.length does not: some characters have a length of 2 | ||
1204 | // We always use the .length so we need to truncate more if needed | ||
1205 | options.length -= maxLength - truncatedDescription.length | ||
1206 | return truncate(this.description, options) | ||
1207 | } | 1198 | } |
1208 | 1199 | ||
1209 | optimizeOriginalVideofile = async function () { | 1200 | optimizeOriginalVideofile = async function () { |