diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-19 16:02:49 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-07-21 17:38:13 +0200 |
commit | 12dc3a942a13c7f1489822dae052da197ef15905 (patch) | |
tree | 7b87b6be692af0b62ebac17e720c80244fd8a7ec /shared/server-commands/videos | |
parent | c6867725fb8e3dfbc2018a37ed5a963103587cb6 (diff) | |
download | PeerTube-12dc3a942a13c7f1489822dae052da197ef15905.tar.gz PeerTube-12dc3a942a13c7f1489822dae052da197ef15905.tar.zst PeerTube-12dc3a942a13c7f1489822dae052da197ef15905.zip |
Implement replace file in server side
Diffstat (limited to 'shared/server-commands/videos')
-rw-r--r-- | shared/server-commands/videos/videos-command.ts | 71 |
1 files changed, 58 insertions, 13 deletions
diff --git a/shared/server-commands/videos/videos-command.ts b/shared/server-commands/videos/videos-command.ts index 9602fa7da..6c38fa7ef 100644 --- a/shared/server-commands/videos/videos-command.ts +++ b/shared/server-commands/videos/videos-command.ts | |||
@@ -32,6 +32,7 @@ export type VideoEdit = Partial<Omit<VideoCreate, 'thumbnailfile' | 'previewfile | |||
32 | } | 32 | } |
33 | 33 | ||
34 | export class VideosCommand extends AbstractCommand { | 34 | export class VideosCommand extends AbstractCommand { |
35 | |||
35 | getCategories (options: OverrideCommandOptions = {}) { | 36 | getCategories (options: OverrideCommandOptions = {}) { |
36 | const path = '/api/v1/videos/categories' | 37 | const path = '/api/v1/videos/categories' |
37 | 38 | ||
@@ -424,7 +425,7 @@ export class VideosCommand extends AbstractCommand { | |||
424 | 425 | ||
425 | const created = mode === 'legacy' | 426 | const created = mode === 'legacy' |
426 | ? await this.buildLegacyUpload({ ...options, attributes }) | 427 | ? await this.buildLegacyUpload({ ...options, attributes }) |
427 | : await this.buildResumeUpload({ ...options, attributes }) | 428 | : await this.buildResumeUpload({ ...options, path: '/api/v1/videos/upload-resumable', attributes }) |
428 | 429 | ||
429 | // Wait torrent generation | 430 | // Wait torrent generation |
430 | const expectedStatus = this.buildExpectedStatus({ ...options, defaultExpectedStatus: HttpStatusCode.OK_200 }) | 431 | const expectedStatus = this.buildExpectedStatus({ ...options, defaultExpectedStatus: HttpStatusCode.OK_200 }) |
@@ -458,9 +459,10 @@ export class VideosCommand extends AbstractCommand { | |||
458 | } | 459 | } |
459 | 460 | ||
460 | async buildResumeUpload (options: OverrideCommandOptions & { | 461 | async buildResumeUpload (options: OverrideCommandOptions & { |
461 | attributes: VideoEdit | 462 | path: string |
463 | attributes: { fixture?: string } & { [id: string]: any } | ||
462 | }): Promise<VideoCreateResult> { | 464 | }): Promise<VideoCreateResult> { |
463 | const { attributes, expectedStatus } = options | 465 | const { path, attributes, expectedStatus } = options |
464 | 466 | ||
465 | let size = 0 | 467 | let size = 0 |
466 | let videoFilePath: string | 468 | let videoFilePath: string |
@@ -478,7 +480,15 @@ export class VideosCommand extends AbstractCommand { | |||
478 | } | 480 | } |
479 | 481 | ||
480 | // Do not check status automatically, we'll check it manually | 482 | // Do not check status automatically, we'll check it manually |
481 | const initializeSessionRes = await this.prepareResumableUpload({ ...options, expectedStatus: null, attributes, size, mimetype }) | 483 | const initializeSessionRes = await this.prepareResumableUpload({ |
484 | ...options, | ||
485 | |||
486 | path, | ||
487 | expectedStatus: null, | ||
488 | attributes, | ||
489 | size, | ||
490 | mimetype | ||
491 | }) | ||
482 | const initStatus = initializeSessionRes.status | 492 | const initStatus = initializeSessionRes.status |
483 | 493 | ||
484 | if (videoFilePath && initStatus === HttpStatusCode.CREATED_201) { | 494 | if (videoFilePath && initStatus === HttpStatusCode.CREATED_201) { |
@@ -487,10 +497,23 @@ export class VideosCommand extends AbstractCommand { | |||
487 | 497 | ||
488 | const pathUploadId = locationHeader.split('?')[1] | 498 | const pathUploadId = locationHeader.split('?')[1] |
489 | 499 | ||
490 | const result = await this.sendResumableChunks({ ...options, pathUploadId, videoFilePath, size }) | 500 | const result = await this.sendResumableChunks({ |
501 | ...options, | ||
502 | |||
503 | path, | ||
504 | pathUploadId, | ||
505 | videoFilePath, | ||
506 | size | ||
507 | }) | ||
491 | 508 | ||
492 | if (result.statusCode === HttpStatusCode.OK_200) { | 509 | if (result.statusCode === HttpStatusCode.OK_200) { |
493 | await this.endResumableUpload({ ...options, expectedStatus: HttpStatusCode.NO_CONTENT_204, pathUploadId }) | 510 | await this.endResumableUpload({ |
511 | ...options, | ||
512 | |||
513 | expectedStatus: HttpStatusCode.NO_CONTENT_204, | ||
514 | path, | ||
515 | pathUploadId | ||
516 | }) | ||
494 | } | 517 | } |
495 | 518 | ||
496 | return result.body?.video || result.body as any | 519 | return result.body?.video || result.body as any |
@@ -506,18 +529,19 @@ export class VideosCommand extends AbstractCommand { | |||
506 | } | 529 | } |
507 | 530 | ||
508 | async prepareResumableUpload (options: OverrideCommandOptions & { | 531 | async prepareResumableUpload (options: OverrideCommandOptions & { |
509 | attributes: VideoEdit | 532 | path: string |
533 | attributes: { fixture?: string } & { [id: string]: any } | ||
510 | size: number | 534 | size: number |
511 | mimetype: string | 535 | mimetype: string |
512 | 536 | ||
513 | originalName?: string | 537 | originalName?: string |
514 | lastModified?: number | 538 | lastModified?: number |
515 | }) { | 539 | }) { |
516 | const { attributes, originalName, lastModified, size, mimetype } = options | 540 | const { path, attributes, originalName, lastModified, size, mimetype } = options |
517 | 541 | ||
518 | const path = '/api/v1/videos/upload-resumable' | 542 | const attaches = this.buildUploadAttaches(omit(options.attributes, [ 'fixture' ])) |
519 | 543 | ||
520 | return this.postUploadRequest({ | 544 | const uploadOptions = { |
521 | ...options, | 545 | ...options, |
522 | 546 | ||
523 | path, | 547 | path, |
@@ -538,11 +562,16 @@ export class VideosCommand extends AbstractCommand { | |||
538 | implicitToken: true, | 562 | implicitToken: true, |
539 | 563 | ||
540 | defaultExpectedStatus: null | 564 | defaultExpectedStatus: null |
541 | }) | 565 | } |
566 | |||
567 | if (Object.keys(attaches).length === 0) return this.postBodyRequest(uploadOptions) | ||
568 | |||
569 | return this.postUploadRequest(uploadOptions) | ||
542 | } | 570 | } |
543 | 571 | ||
544 | sendResumableChunks (options: OverrideCommandOptions & { | 572 | sendResumableChunks (options: OverrideCommandOptions & { |
545 | pathUploadId: string | 573 | pathUploadId: string |
574 | path: string | ||
546 | videoFilePath: string | 575 | videoFilePath: string |
547 | size: number | 576 | size: number |
548 | contentLength?: number | 577 | contentLength?: number |
@@ -550,6 +579,7 @@ export class VideosCommand extends AbstractCommand { | |||
550 | digestBuilder?: (chunk: any) => string | 579 | digestBuilder?: (chunk: any) => string |
551 | }) { | 580 | }) { |
552 | const { | 581 | const { |
582 | path, | ||
553 | pathUploadId, | 583 | pathUploadId, |
554 | videoFilePath, | 584 | videoFilePath, |
555 | size, | 585 | size, |
@@ -559,7 +589,6 @@ export class VideosCommand extends AbstractCommand { | |||
559 | expectedStatus = HttpStatusCode.OK_200 | 589 | expectedStatus = HttpStatusCode.OK_200 |
560 | } = options | 590 | } = options |
561 | 591 | ||
562 | const path = '/api/v1/videos/upload-resumable' | ||
563 | let start = 0 | 592 | let start = 0 |
564 | 593 | ||
565 | const token = this.buildCommonRequestToken({ ...options, implicitToken: true }) | 594 | const token = this.buildCommonRequestToken({ ...options, implicitToken: true }) |
@@ -610,12 +639,13 @@ export class VideosCommand extends AbstractCommand { | |||
610 | } | 639 | } |
611 | 640 | ||
612 | endResumableUpload (options: OverrideCommandOptions & { | 641 | endResumableUpload (options: OverrideCommandOptions & { |
642 | path: string | ||
613 | pathUploadId: string | 643 | pathUploadId: string |
614 | }) { | 644 | }) { |
615 | return this.deleteRequest({ | 645 | return this.deleteRequest({ |
616 | ...options, | 646 | ...options, |
617 | 647 | ||
618 | path: '/api/v1/videos/upload-resumable', | 648 | path: options.path, |
619 | rawQuery: options.pathUploadId, | 649 | rawQuery: options.pathUploadId, |
620 | implicitToken: true, | 650 | implicitToken: true, |
621 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | 651 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 |
@@ -657,6 +687,21 @@ export class VideosCommand extends AbstractCommand { | |||
657 | 687 | ||
658 | // --------------------------------------------------------------------------- | 688 | // --------------------------------------------------------------------------- |
659 | 689 | ||
690 | replaceSourceFile (options: OverrideCommandOptions & { | ||
691 | videoId: number | string | ||
692 | fixture: string | ||
693 | }) { | ||
694 | return this.buildResumeUpload({ | ||
695 | ...options, | ||
696 | |||
697 | path: '/api/v1/videos/' + options.videoId + '/source/replace-resumable', | ||
698 | attributes: { fixture: options.fixture }, | ||
699 | expectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
700 | }) | ||
701 | } | ||
702 | |||
703 | // --------------------------------------------------------------------------- | ||
704 | |||
660 | removeHLSPlaylist (options: OverrideCommandOptions & { | 705 | removeHLSPlaylist (options: OverrideCommandOptions & { |
661 | videoId: number | string | 706 | videoId: number | string |
662 | }) { | 707 | }) { |