diff options
author | Chocobozzz <me@florianbigard.com> | 2021-05-11 14:56:00 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-05-11 14:56:30 +0200 |
commit | 32985a0a779e0e2100a3990b1f1188645e58dfb1 (patch) | |
tree | f96e0166858b10642e2cbf97906d63c57b85ebc1 | |
parent | a3c997b34cad79fce92c67951a3c8140a9888973 (diff) | |
download | PeerTube-32985a0a779e0e2100a3990b1f1188645e58dfb1.tar.gz PeerTube-32985a0a779e0e2100a3990b1f1188645e58dfb1.tar.zst PeerTube-32985a0a779e0e2100a3990b1f1188645e58dfb1.zip |
Error if importing a torrent with multiple files
4 files changed, 24 insertions, 10 deletions
diff --git a/client/src/app/+videos/+video-edit/video-add-components/video-import-torrent.component.ts b/client/src/app/+videos/+video-edit/video-add-components/video-import-torrent.component.ts index 3aae24732..23bd5ef76 100644 --- a/client/src/app/+videos/+video-edit/video-add-components/video-import-torrent.component.ts +++ b/client/src/app/+videos/+video-edit/video-add-components/video-import-torrent.component.ts | |||
@@ -5,7 +5,7 @@ import { scrollToTop } from '@app/helpers' | |||
5 | import { FormValidatorService } from '@app/shared/shared-forms' | 5 | import { FormValidatorService } from '@app/shared/shared-forms' |
6 | import { VideoCaptionService, VideoEdit, VideoImportService, VideoService } from '@app/shared/shared-main' | 6 | import { VideoCaptionService, VideoEdit, VideoImportService, VideoService } from '@app/shared/shared-main' |
7 | import { LoadingBarService } from '@ngx-loading-bar/core' | 7 | import { LoadingBarService } from '@ngx-loading-bar/core' |
8 | import { VideoPrivacy, VideoUpdate } from '@shared/models' | 8 | import { ServerErrorCode, VideoPrivacy, VideoUpdate } from '@shared/models' |
9 | import { hydrateFormFromVideo } from '../shared/video-edit-utils' | 9 | import { hydrateFormFromVideo } from '../shared/video-edit-utils' |
10 | import { VideoSend } from './video-send' | 10 | import { VideoSend } from './video-send' |
11 | 11 | ||
@@ -113,7 +113,13 @@ export class VideoImportTorrentComponent extends VideoSend implements OnInit, Af | |||
113 | this.loadingBar.useRef().complete() | 113 | this.loadingBar.useRef().complete() |
114 | this.isImportingVideo = false | 114 | this.isImportingVideo = false |
115 | this.firstStepError.emit() | 115 | this.firstStepError.emit() |
116 | this.notifier.error(err.message) | 116 | |
117 | let message = err.message | ||
118 | if (err.body?.code === ServerErrorCode.INCORRECT_FILES_IN_TORRENT) { | ||
119 | message = $localize`Torrents with only 1 file are supported.` | ||
120 | } | ||
121 | |||
122 | this.notifier.error(message) | ||
117 | } | 123 | } |
118 | ) | 124 | ) |
119 | } | 125 | } |
diff --git a/server/controllers/api/videos/import.ts b/server/controllers/api/videos/import.ts index 37fd42b90..4ed58f978 100644 --- a/server/controllers/api/videos/import.ts +++ b/server/controllers/api/videos/import.ts | |||
@@ -16,13 +16,13 @@ import { | |||
16 | MVideoWithBlacklistLight | 16 | MVideoWithBlacklistLight |
17 | } from '@server/types/models' | 17 | } from '@server/types/models' |
18 | import { MVideoImport, MVideoImportFormattable } from '@server/types/models/video/video-import' | 18 | import { MVideoImport, MVideoImportFormattable } from '@server/types/models/video/video-import' |
19 | import { VideoImportCreate, VideoImportState, VideoPrivacy, VideoState } from '../../../../shared' | 19 | import { ServerErrorCode, VideoImportCreate, VideoImportState, VideoPrivacy, VideoState } from '../../../../shared' |
20 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' | 20 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' |
21 | import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type' | 21 | import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type' |
22 | import { auditLoggerFactory, getAuditIdFromRes, VideoImportAuditView } from '../../../helpers/audit-logger' | 22 | import { auditLoggerFactory, getAuditIdFromRes, VideoImportAuditView } from '../../../helpers/audit-logger' |
23 | import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils' | 23 | import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils' |
24 | import { isArray } from '../../../helpers/custom-validators/misc' | 24 | import { isArray } from '../../../helpers/custom-validators/misc' |
25 | import { createReqFiles } from '../../../helpers/express-utils' | 25 | import { cleanUpReqFiles, createReqFiles } from '../../../helpers/express-utils' |
26 | import { logger } from '../../../helpers/logger' | 26 | import { logger } from '../../../helpers/logger' |
27 | import { getSecureTorrentName } from '../../../helpers/utils' | 27 | import { getSecureTorrentName } from '../../../helpers/utils' |
28 | import { YoutubeDL, YoutubeDLInfo } from '../../../helpers/youtube-dl' | 28 | import { YoutubeDL, YoutubeDLInfo } from '../../../helpers/youtube-dl' |
@@ -86,13 +86,23 @@ async function addTorrentImport (req: express.Request, res: express.Response, to | |||
86 | 86 | ||
87 | // Rename the torrent to a secured name | 87 | // Rename the torrent to a secured name |
88 | const newTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, getSecureTorrentName(torrentName)) | 88 | const newTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, getSecureTorrentName(torrentName)) |
89 | await move(torrentfile.path, newTorrentPath) | 89 | await move(torrentfile.path, newTorrentPath, { overwrite: true }) |
90 | torrentfile.path = newTorrentPath | 90 | torrentfile.path = newTorrentPath |
91 | 91 | ||
92 | const buf = await readFile(torrentfile.path) | 92 | const buf = await readFile(torrentfile.path) |
93 | const parsedTorrent = parseTorrent(buf) | 93 | const parsedTorrent = parseTorrent(buf) as parseTorrent.Instance |
94 | 94 | ||
95 | videoName = isArray(parsedTorrent.name) ? parsedTorrent.name[0] : parsedTorrent.name as string | 95 | if (parsedTorrent.files.length !== 1) { |
96 | cleanUpReqFiles(req) | ||
97 | |||
98 | return res.status(HttpStatusCode.BAD_REQUEST_400) | ||
99 | .json({ | ||
100 | code: ServerErrorCode.INCORRECT_FILES_IN_TORRENT, | ||
101 | error: 'Torrents with only 1 file are supported.' | ||
102 | }) | ||
103 | } | ||
104 | |||
105 | videoName = isArray(parsedTorrent.name) ? parsedTorrent.name[0] : parsedTorrent.name | ||
96 | } else { | 106 | } else { |
97 | magnetUri = body.magnetUri | 107 | magnetUri = body.magnetUri |
98 | 108 | ||
diff --git a/server/middlewares/validators/videos/video-imports.ts b/server/middlewares/validators/videos/video-imports.ts index c53af3861..d0643ff26 100644 --- a/server/middlewares/validators/videos/video-imports.ts +++ b/server/middlewares/validators/videos/video-imports.ts | |||
@@ -47,14 +47,12 @@ const videoImportAddValidator = getCommonVideoEditAttributes().concat([ | |||
47 | cleanUpReqFiles(req) | 47 | cleanUpReqFiles(req) |
48 | return res.status(HttpStatusCode.CONFLICT_409) | 48 | return res.status(HttpStatusCode.CONFLICT_409) |
49 | .json({ error: 'HTTP import is not enabled on this instance.' }) | 49 | .json({ error: 'HTTP import is not enabled on this instance.' }) |
50 | .end() | ||
51 | } | 50 | } |
52 | 51 | ||
53 | if (CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED !== true && (req.body.magnetUri || torrentFile)) { | 52 | if (CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED !== true && (req.body.magnetUri || torrentFile)) { |
54 | cleanUpReqFiles(req) | 53 | cleanUpReqFiles(req) |
55 | return res.status(HttpStatusCode.CONFLICT_409) | 54 | return res.status(HttpStatusCode.CONFLICT_409) |
56 | .json({ error: 'Torrent/magnet URI import is not enabled on this instance.' }) | 55 | .json({ error: 'Torrent/magnet URI import is not enabled on this instance.' }) |
57 | .end() | ||
58 | } | 56 | } |
59 | 57 | ||
60 | if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) | 58 | if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) |
@@ -65,7 +63,6 @@ const videoImportAddValidator = getCommonVideoEditAttributes().concat([ | |||
65 | 63 | ||
66 | return res.status(HttpStatusCode.BAD_REQUEST_400) | 64 | return res.status(HttpStatusCode.BAD_REQUEST_400) |
67 | .json({ error: 'Should have a magnetUri or a targetUrl or a torrent file.' }) | 65 | .json({ error: 'Should have a magnetUri or a targetUrl or a torrent file.' }) |
68 | .end() | ||
69 | } | 66 | } |
70 | 67 | ||
71 | if (!await isImportAccepted(req, res)) return cleanUpReqFiles(req) | 68 | if (!await isImportAccepted(req, res)) return cleanUpReqFiles(req) |
diff --git a/shared/models/server/server-error-code.enum.ts b/shared/models/server/server-error-code.enum.ts index c02b0e6c7..d17d958be 100644 --- a/shared/models/server/server-error-code.enum.ts +++ b/shared/models/server/server-error-code.enum.ts | |||
@@ -2,4 +2,5 @@ export const enum ServerErrorCode { | |||
2 | DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS = 1, | 2 | DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS = 1, |
3 | MAX_INSTANCE_LIVES_LIMIT_REACHED = 2, | 3 | MAX_INSTANCE_LIVES_LIMIT_REACHED = 2, |
4 | MAX_USER_LIVES_LIMIT_REACHED = 3, | 4 | MAX_USER_LIVES_LIMIT_REACHED = 3, |
5 | INCORRECT_FILES_IN_TORRENT = 4 | ||
5 | } | 6 | } |