diff options
Diffstat (limited to 'server/controllers/api/videos/index.ts')
-rw-r--r-- | server/controllers/api/videos/index.ts | 48 |
1 files changed, 21 insertions, 27 deletions
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 2b7ead954..ec855ee8e 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts | |||
@@ -46,6 +46,7 @@ import { VideoCreate, VideoUpdate } from '../../../../shared' | |||
46 | import { abuseVideoRouter } from './abuse' | 46 | import { abuseVideoRouter } from './abuse' |
47 | import { blacklistRouter } from './blacklist' | 47 | import { blacklistRouter } from './blacklist' |
48 | import { rateVideoRouter } from './rate' | 48 | import { rateVideoRouter } from './rate' |
49 | import { videoChannelRouter } from './channel' | ||
49 | 50 | ||
50 | const videosRouter = express.Router() | 51 | const videosRouter = express.Router() |
51 | 52 | ||
@@ -76,6 +77,7 @@ const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCo | |||
76 | videosRouter.use('/', abuseVideoRouter) | 77 | videosRouter.use('/', abuseVideoRouter) |
77 | videosRouter.use('/', blacklistRouter) | 78 | videosRouter.use('/', blacklistRouter) |
78 | videosRouter.use('/', rateVideoRouter) | 79 | videosRouter.use('/', rateVideoRouter) |
80 | videosRouter.use('/', videoChannelRouter) | ||
79 | 81 | ||
80 | videosRouter.get('/categories', listVideoCategories) | 82 | videosRouter.get('/categories', listVideoCategories) |
81 | videosRouter.get('/licences', listVideoLicences) | 83 | videosRouter.get('/licences', listVideoLicences) |
@@ -161,21 +163,13 @@ function addVideo (req: express.Request, res: express.Response, videoPhysicalFil | |||
161 | let videoUUID = '' | 163 | let videoUUID = '' |
162 | 164 | ||
163 | return db.sequelize.transaction(t => { | 165 | return db.sequelize.transaction(t => { |
164 | const user = res.locals.oauth.token.User | 166 | let p: Promise<TagInstance[]> |
165 | 167 | ||
166 | const name = user.username | 168 | if (!videoInfo.tags) p = Promise.resolve(undefined) |
167 | // null because it is OUR pod | 169 | else p = db.Tag.findOrCreateTags(videoInfo.tags, t) |
168 | const podId = null | ||
169 | const userId = user.id | ||
170 | 170 | ||
171 | return db.Author.findOrCreateAuthor(name, podId, userId, t) | 171 | return p |
172 | .then(author => { | 172 | .then(tagInstances => { |
173 | const tags = videoInfo.tags | ||
174 | if (!tags) return { author, tagInstances: undefined } | ||
175 | |||
176 | return db.Tag.findOrCreateTags(tags, t).then(tagInstances => ({ author, tagInstances })) | ||
177 | }) | ||
178 | .then(({ author, tagInstances }) => { | ||
179 | const videoData = { | 173 | const videoData = { |
180 | name: videoInfo.name, | 174 | name: videoInfo.name, |
181 | remote: false, | 175 | remote: false, |
@@ -186,18 +180,18 @@ function addVideo (req: express.Request, res: express.Response, videoPhysicalFil | |||
186 | nsfw: videoInfo.nsfw, | 180 | nsfw: videoInfo.nsfw, |
187 | description: videoInfo.description, | 181 | description: videoInfo.description, |
188 | duration: videoPhysicalFile['duration'], // duration was added by a previous middleware | 182 | duration: videoPhysicalFile['duration'], // duration was added by a previous middleware |
189 | authorId: author.id | 183 | channelId: res.locals.videoChannel.id |
190 | } | 184 | } |
191 | 185 | ||
192 | const video = db.Video.build(videoData) | 186 | const video = db.Video.build(videoData) |
193 | return { author, tagInstances, video } | 187 | return { tagInstances, video } |
194 | }) | 188 | }) |
195 | .then(({ author, tagInstances, video }) => { | 189 | .then(({ tagInstances, video }) => { |
196 | const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename) | 190 | const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename) |
197 | return getVideoFileHeight(videoFilePath) | 191 | return getVideoFileHeight(videoFilePath) |
198 | .then(height => ({ author, tagInstances, video, videoFileHeight: height })) | 192 | .then(height => ({ tagInstances, video, videoFileHeight: height })) |
199 | }) | 193 | }) |
200 | .then(({ author, tagInstances, video, videoFileHeight }) => { | 194 | .then(({ tagInstances, video, videoFileHeight }) => { |
201 | const videoFileData = { | 195 | const videoFileData = { |
202 | extname: extname(videoPhysicalFile.filename), | 196 | extname: extname(videoPhysicalFile.filename), |
203 | resolution: videoFileHeight, | 197 | resolution: videoFileHeight, |
@@ -205,9 +199,9 @@ function addVideo (req: express.Request, res: express.Response, videoPhysicalFil | |||
205 | } | 199 | } |
206 | 200 | ||
207 | const videoFile = db.VideoFile.build(videoFileData) | 201 | const videoFile = db.VideoFile.build(videoFileData) |
208 | return { author, tagInstances, video, videoFile } | 202 | return { tagInstances, video, videoFile } |
209 | }) | 203 | }) |
210 | .then(({ author, tagInstances, video, videoFile }) => { | 204 | .then(({ tagInstances, video, videoFile }) => { |
211 | const videoDir = CONFIG.STORAGE.VIDEOS_DIR | 205 | const videoDir = CONFIG.STORAGE.VIDEOS_DIR |
212 | const source = join(videoDir, videoPhysicalFile.filename) | 206 | const source = join(videoDir, videoPhysicalFile.filename) |
213 | const destination = join(videoDir, video.getVideoFilename(videoFile)) | 207 | const destination = join(videoDir, video.getVideoFilename(videoFile)) |
@@ -216,10 +210,10 @@ function addVideo (req: express.Request, res: express.Response, videoPhysicalFil | |||
216 | .then(() => { | 210 | .then(() => { |
217 | // This is important in case if there is another attempt in the retry process | 211 | // This is important in case if there is another attempt in the retry process |
218 | videoPhysicalFile.filename = video.getVideoFilename(videoFile) | 212 | videoPhysicalFile.filename = video.getVideoFilename(videoFile) |
219 | return { author, tagInstances, video, videoFile } | 213 | return { tagInstances, video, videoFile } |
220 | }) | 214 | }) |
221 | }) | 215 | }) |
222 | .then(({ author, tagInstances, video, videoFile }) => { | 216 | .then(({ tagInstances, video, videoFile }) => { |
223 | const tasks = [] | 217 | const tasks = [] |
224 | 218 | ||
225 | tasks.push( | 219 | tasks.push( |
@@ -239,15 +233,15 @@ function addVideo (req: express.Request, res: express.Response, videoPhysicalFil | |||
239 | ) | 233 | ) |
240 | } | 234 | } |
241 | 235 | ||
242 | return Promise.all(tasks).then(() => ({ author, tagInstances, video, videoFile })) | 236 | return Promise.all(tasks).then(() => ({ tagInstances, video, videoFile })) |
243 | }) | 237 | }) |
244 | .then(({ author, tagInstances, video, videoFile }) => { | 238 | .then(({ tagInstances, video, videoFile }) => { |
245 | const options = { transaction: t } | 239 | const options = { transaction: t } |
246 | 240 | ||
247 | return video.save(options) | 241 | return video.save(options) |
248 | .then(videoCreated => { | 242 | .then(videoCreated => { |
249 | // Do not forget to add Author information to the created video | 243 | // Do not forget to add video channel information to the created video |
250 | videoCreated.Author = author | 244 | videoCreated.VideoChannel = res.locals.videoChannel |
251 | videoUUID = videoCreated.uuid | 245 | videoUUID = videoCreated.uuid |
252 | 246 | ||
253 | return { tagInstances, video: videoCreated, videoFile } | 247 | return { tagInstances, video: videoCreated, videoFile } |
@@ -392,7 +386,7 @@ function getVideo (req: express.Request, res: express.Response) { | |||
392 | } | 386 | } |
393 | 387 | ||
394 | // Do not wait the view system | 388 | // Do not wait the view system |
395 | res.json(videoInstance.toFormattedJSON()) | 389 | res.json(videoInstance.toFormattedDetailsJSON()) |
396 | } | 390 | } |
397 | 391 | ||
398 | function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) { | 392 | function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) { |