diff options
author | Chocobozzz <me@florianbigard.com> | 2019-04-17 10:07:00 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-04-24 16:25:52 +0200 |
commit | e8bafea35bc930cb8ac5b2d521a188642a1adffe (patch) | |
tree | 7537f957ed7307b464e3c90b71b813d992acaade /server/controllers/api/video-playlist.ts | |
parent | 94565d52bb2883e09f16d1363170ac9c0dccb7a1 (diff) | |
download | PeerTube-e8bafea35bc930cb8ac5b2d521a188642a1adffe.tar.gz PeerTube-e8bafea35bc930cb8ac5b2d521a188642a1adffe.tar.zst PeerTube-e8bafea35bc930cb8ac5b2d521a188642a1adffe.zip |
Create a dedicated table to track video thumbnails
Diffstat (limited to 'server/controllers/api/video-playlist.ts')
-rw-r--r-- | server/controllers/api/video-playlist.ts | 47 |
1 files changed, 25 insertions, 22 deletions
diff --git a/server/controllers/api/video-playlist.ts b/server/controllers/api/video-playlist.ts index 71c244a60..99325aa9d 100644 --- a/server/controllers/api/video-playlist.ts +++ b/server/controllers/api/video-playlist.ts | |||
@@ -12,7 +12,7 @@ import { | |||
12 | } from '../../middlewares' | 12 | } from '../../middlewares' |
13 | import { videoPlaylistsSortValidator } from '../../middlewares/validators' | 13 | import { videoPlaylistsSortValidator } from '../../middlewares/validators' |
14 | import { buildNSFWFilter, createReqFiles, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils' | 14 | import { buildNSFWFilter, createReqFiles, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils' |
15 | import { MIMETYPES, THUMBNAILS_SIZE, VIDEO_PLAYLIST_PRIVACIES } from '../../initializers/constants' | 15 | import { MIMETYPES, VIDEO_PLAYLIST_PRIVACIES } from '../../initializers/constants' |
16 | import { logger } from '../../helpers/logger' | 16 | import { logger } from '../../helpers/logger' |
17 | import { resetSequelizeInstance } from '../../helpers/database-utils' | 17 | import { resetSequelizeInstance } from '../../helpers/database-utils' |
18 | import { VideoPlaylistModel } from '../../models/video/video-playlist' | 18 | import { VideoPlaylistModel } from '../../models/video/video-playlist' |
@@ -28,7 +28,6 @@ import { | |||
28 | } from '../../middlewares/validators/videos/video-playlists' | 28 | } from '../../middlewares/validators/videos/video-playlists' |
29 | import { VideoPlaylistCreate } from '../../../shared/models/videos/playlist/video-playlist-create.model' | 29 | import { VideoPlaylistCreate } from '../../../shared/models/videos/playlist/video-playlist-create.model' |
30 | import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' | 30 | import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' |
31 | import { processImage } from '../../helpers/image-utils' | ||
32 | import { join } from 'path' | 31 | import { join } from 'path' |
33 | import { sendCreateVideoPlaylist, sendDeleteVideoPlaylist, sendUpdateVideoPlaylist } from '../../lib/activitypub/send' | 32 | import { sendCreateVideoPlaylist, sendDeleteVideoPlaylist, sendUpdateVideoPlaylist } from '../../lib/activitypub/send' |
34 | import { getVideoPlaylistActivityPubUrl, getVideoPlaylistElementActivityPubUrl } from '../../lib/activitypub/url' | 33 | import { getVideoPlaylistActivityPubUrl, getVideoPlaylistElementActivityPubUrl } from '../../lib/activitypub/url' |
@@ -37,12 +36,12 @@ import { VideoModel } from '../../models/video/video' | |||
37 | import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element' | 36 | import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element' |
38 | import { VideoPlaylistElementCreate } from '../../../shared/models/videos/playlist/video-playlist-element-create.model' | 37 | import { VideoPlaylistElementCreate } from '../../../shared/models/videos/playlist/video-playlist-element-create.model' |
39 | import { VideoPlaylistElementUpdate } from '../../../shared/models/videos/playlist/video-playlist-element-update.model' | 38 | import { VideoPlaylistElementUpdate } from '../../../shared/models/videos/playlist/video-playlist-element-update.model' |
40 | import { copy, pathExists } from 'fs-extra' | ||
41 | import { AccountModel } from '../../models/account/account' | 39 | import { AccountModel } from '../../models/account/account' |
42 | import { VideoPlaylistReorder } from '../../../shared/models/videos/playlist/video-playlist-reorder.model' | 40 | import { VideoPlaylistReorder } from '../../../shared/models/videos/playlist/video-playlist-reorder.model' |
43 | import { JobQueue } from '../../lib/job-queue' | 41 | import { JobQueue } from '../../lib/job-queue' |
44 | import { CONFIG } from '../../initializers/config' | 42 | import { CONFIG } from '../../initializers/config' |
45 | import { sequelizeTypescript } from '../../initializers/database' | 43 | import { sequelizeTypescript } from '../../initializers/database' |
44 | import { createPlaylistThumbnailFromExisting } from '../../lib/thumbnail' | ||
46 | 45 | ||
47 | const reqThumbnailFile = createReqFiles([ 'thumbnailfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { thumbnailfile: CONFIG.STORAGE.TMP_DIR }) | 46 | const reqThumbnailFile = createReqFiles([ 'thumbnailfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { thumbnailfile: CONFIG.STORAGE.TMP_DIR }) |
48 | 47 | ||
@@ -174,14 +173,18 @@ async function addVideoPlaylist (req: express.Request, res: express.Response) { | |||
174 | } | 173 | } |
175 | 174 | ||
176 | const thumbnailField = req.files['thumbnailfile'] | 175 | const thumbnailField = req.files['thumbnailfile'] |
177 | if (thumbnailField) { | 176 | const thumbnailModel = thumbnailField |
178 | const thumbnailPhysicalFile = thumbnailField[ 0 ] | 177 | ? await createPlaylistThumbnailFromExisting(thumbnailField[0].path, videoPlaylist) |
179 | await processImage(thumbnailPhysicalFile, join(CONFIG.STORAGE.THUMBNAILS_DIR, videoPlaylist.getThumbnailName()), THUMBNAILS_SIZE) | 178 | : undefined |
180 | } | ||
181 | 179 | ||
182 | const videoPlaylistCreated: VideoPlaylistModel = await sequelizeTypescript.transaction(async t => { | 180 | const videoPlaylistCreated: VideoPlaylistModel = await sequelizeTypescript.transaction(async t => { |
183 | const videoPlaylistCreated = await videoPlaylist.save({ transaction: t }) | 181 | const videoPlaylistCreated = await videoPlaylist.save({ transaction: t }) |
184 | 182 | ||
183 | if (thumbnailModel) { | ||
184 | thumbnailModel.videoPlaylistId = videoPlaylistCreated.id | ||
185 | videoPlaylistCreated.setThumbnail(await thumbnailModel.save({ transaction: t })) | ||
186 | } | ||
187 | |||
185 | // We need more attributes for the federation | 188 | // We need more attributes for the federation |
186 | videoPlaylistCreated.OwnerAccount = await AccountModel.load(user.Account.id, t) | 189 | videoPlaylistCreated.OwnerAccount = await AccountModel.load(user.Account.id, t) |
187 | await sendCreateVideoPlaylist(videoPlaylistCreated, t) | 190 | await sendCreateVideoPlaylist(videoPlaylistCreated, t) |
@@ -206,14 +209,9 @@ async function updateVideoPlaylist (req: express.Request, res: express.Response) | |||
206 | const wasPrivatePlaylist = videoPlaylistInstance.privacy === VideoPlaylistPrivacy.PRIVATE | 209 | const wasPrivatePlaylist = videoPlaylistInstance.privacy === VideoPlaylistPrivacy.PRIVATE |
207 | 210 | ||
208 | const thumbnailField = req.files['thumbnailfile'] | 211 | const thumbnailField = req.files['thumbnailfile'] |
209 | if (thumbnailField) { | 212 | const thumbnailModel = thumbnailField |
210 | const thumbnailPhysicalFile = thumbnailField[ 0 ] | 213 | ? await createPlaylistThumbnailFromExisting(thumbnailField[0].path, videoPlaylistInstance) |
211 | await processImage( | 214 | : undefined |
212 | thumbnailPhysicalFile, | ||
213 | join(CONFIG.STORAGE.THUMBNAILS_DIR, videoPlaylistInstance.getThumbnailName()), | ||
214 | THUMBNAILS_SIZE | ||
215 | ) | ||
216 | } | ||
217 | 215 | ||
218 | try { | 216 | try { |
219 | await sequelizeTypescript.transaction(async t => { | 217 | await sequelizeTypescript.transaction(async t => { |
@@ -241,6 +239,11 @@ async function updateVideoPlaylist (req: express.Request, res: express.Response) | |||
241 | 239 | ||
242 | const playlistUpdated = await videoPlaylistInstance.save(sequelizeOptions) | 240 | const playlistUpdated = await videoPlaylistInstance.save(sequelizeOptions) |
243 | 241 | ||
242 | if (thumbnailModel) { | ||
243 | thumbnailModel.videoPlaylistId = playlistUpdated.id | ||
244 | playlistUpdated.setThumbnail(await thumbnailModel.save({ transaction: t })) | ||
245 | } | ||
246 | |||
244 | const isNewPlaylist = wasPrivatePlaylist && playlistUpdated.privacy !== VideoPlaylistPrivacy.PRIVATE | 247 | const isNewPlaylist = wasPrivatePlaylist && playlistUpdated.privacy !== VideoPlaylistPrivacy.PRIVATE |
245 | 248 | ||
246 | if (isNewPlaylist) { | 249 | if (isNewPlaylist) { |
@@ -307,15 +310,15 @@ async function addVideoInPlaylist (req: express.Request, res: express.Response) | |||
307 | }) | 310 | }) |
308 | 311 | ||
309 | // If the user did not set a thumbnail, automatically take the video thumbnail | 312 | // If the user did not set a thumbnail, automatically take the video thumbnail |
310 | if (playlistElement.position === 1) { | 313 | if (playlistElement.position === 1 && videoPlaylist.hasThumbnail() === false) { |
311 | const playlistThumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, videoPlaylist.getThumbnailName()) | 314 | logger.info('Generating default thumbnail to playlist %s.', videoPlaylist.url) |
312 | 315 | ||
313 | if (await pathExists(playlistThumbnailPath) === false) { | 316 | const inputPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnail().filename) |
314 | logger.info('Generating default thumbnail to playlist %s.', videoPlaylist.url) | 317 | const thumbnailModel = await createPlaylistThumbnailFromExisting(inputPath, videoPlaylist, true) |
315 | 318 | ||
316 | const videoThumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName()) | 319 | thumbnailModel.videoPlaylistId = videoPlaylist.id |
317 | await copy(videoThumbnailPath, playlistThumbnailPath) | 320 | |
318 | } | 321 | await thumbnailModel.save() |
319 | } | 322 | } |
320 | 323 | ||
321 | logger.info('Video added in playlist %s at position %d.', videoPlaylist.uuid, playlistElement.position) | 324 | logger.info('Video added in playlist %s at position %d.', videoPlaylist.uuid, playlistElement.position) |