]>
Commit | Line | Data |
---|---|---|
0146f351 | 1 | import { switchMap } from 'rxjs' |
2e257e36 | 2 | import { AfterViewInit, Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core' |
ce33919c | 3 | import { Router } from '@angular/router' |
2e257e36 | 4 | import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core' |
67ed6552 | 5 | import { scrollToTop } from '@app/helpers' |
5c5bcea2 | 6 | import { FormReactiveService } from '@app/shared/shared-forms' |
67ed6552 | 7 | import { VideoCaptionService, VideoEdit, VideoImportService, VideoService } from '@app/shared/shared-main' |
67ed6552 | 8 | import { LoadingBarService } from '@ngx-loading-bar/core' |
42b40636 | 9 | import { logger } from '@root-helpers/logger' |
9df52d66 | 10 | import { PeerTubeProblemDocument, ServerErrorCode, VideoUpdate } from '@shared/models' |
c6c0fa6c | 11 | import { hydrateFormFromVideo } from '../shared/video-edit-utils' |
66357162 | 12 | import { VideoSend } from './video-send' |
ce33919c C |
13 | |
14 | @Component({ | |
15 | selector: 'my-video-import-torrent', | |
16 | templateUrl: './video-import-torrent.component.html', | |
17 | styleUrls: [ | |
18 | '../shared/video-edit.component.scss', | |
457bb213 C |
19 | './video-import-torrent.component.scss', |
20 | './video-send.scss' | |
ce33919c C |
21 | ] |
22 | }) | |
2e257e36 | 23 | export class VideoImportTorrentComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate { |
ce33919c | 24 | @Output() firstStepDone = new EventEmitter<string>() |
7373507f | 25 | @Output() firstStepError = new EventEmitter<void>() |
2f5d2ec5 | 26 | @ViewChild('torrentfileInput') torrentfileInput: ElementRef<HTMLInputElement> |
ce33919c | 27 | |
ce33919c C |
28 | magnetUri = '' |
29 | ||
30 | isImportingVideo = false | |
31 | hasImportedVideo = false | |
32 | isUpdatingVideo = false | |
33 | ||
34 | video: VideoEdit | |
7373507f | 35 | error: string |
ce33919c | 36 | |
ce33919c | 37 | constructor ( |
5c5bcea2 | 38 | protected formReactiveService: FormReactiveService, |
ce33919c | 39 | protected loadingBar: LoadingBarService, |
f8b2c1b4 | 40 | protected notifier: Notifier, |
ce33919c C |
41 | protected authService: AuthService, |
42 | protected serverService: ServerService, | |
43 | protected videoService: VideoService, | |
44 | protected videoCaptionService: VideoCaptionService, | |
45 | private router: Router, | |
2e257e36 C |
46 | private videoImportService: VideoImportService, |
47 | private hooks: HooksService | |
9df52d66 | 48 | ) { |
ce33919c C |
49 | super() |
50 | } | |
51 | ||
52 | ngOnInit () { | |
53 | super.ngOnInit() | |
54 | } | |
55 | ||
2e257e36 C |
56 | ngAfterViewInit () { |
57 | this.hooks.runAction('action:video-torrent-import.init', 'video-edit') | |
58 | } | |
59 | ||
ce33919c C |
60 | canDeactivate () { |
61 | return { canDeactivate: true } | |
62 | } | |
63 | ||
64 | isMagnetUrlValid () { | |
65 | return !!this.magnetUri | |
66 | } | |
67 | ||
990b6a0b | 68 | fileChange () { |
c199c427 | 69 | const torrentfile = this.torrentfileInput.nativeElement.files[0] |
990b6a0b C |
70 | if (!torrentfile) return |
71 | ||
72 | this.importVideo(torrentfile) | |
73 | } | |
74 | ||
c9ff8a08 RK |
75 | setTorrentFile (files: FileList) { |
76 | this.torrentfileInput.nativeElement.files = files | |
77 | this.fileChange() | |
78 | } | |
79 | ||
990b6a0b | 80 | importVideo (torrentfile?: Blob) { |
ce33919c C |
81 | this.isImportingVideo = true |
82 | ||
83 | const videoUpdate: VideoUpdate = { | |
a3f45a2a | 84 | privacy: this.highestPrivacy, |
ce33919c | 85 | waitTranscoding: false, |
ce33919c C |
86 | channelId: this.firstStepChannelId |
87 | } | |
88 | ||
a02b93ce | 89 | this.loadingBar.useRef().start() |
ce33919c | 90 | |
1378c0d3 | 91 | this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate) |
0146f351 | 92 | .pipe(switchMap(({ video }) => this.videoService.getVideo({ videoId: video.uuid }))) |
1378c0d3 | 93 | .subscribe({ |
0146f351 | 94 | next: video => { |
1378c0d3 | 95 | this.loadingBar.useRef().complete() |
0146f351 | 96 | this.firstStepDone.emit(video.name) |
1378c0d3 C |
97 | this.isImportingVideo = false |
98 | this.hasImportedVideo = true | |
99 | ||
0146f351 C |
100 | this.video = new VideoEdit(video) |
101 | this.video.patch({ privacy: this.firstStepPrivacyId }) | |
1378c0d3 C |
102 | |
103 | hydrateFormFromVideo(this.form, this.video, false) | |
104 | }, | |
105 | ||
106 | error: err => { | |
107 | this.loadingBar.useRef().complete() | |
108 | this.isImportingVideo = false | |
109 | this.firstStepError.emit() | |
110 | ||
111 | let message = err.message | |
112 | ||
113 | const error = err.body as PeerTubeProblemDocument | |
114 | if (error?.code === ServerErrorCode.INCORRECT_FILES_IN_TORRENT) { | |
115 | message = $localize`Torrents with only 1 file are supported.` | |
116 | } | |
32985a0a | 117 | |
1378c0d3 C |
118 | this.notifier.error(message) |
119 | } | |
120 | }) | |
ce33919c C |
121 | } |
122 | ||
cc4bf76c C |
123 | async updateSecondStep () { |
124 | if (!await this.isFormValid()) return | |
ce33919c C |
125 | |
126 | this.video.patch(this.form.value) | |
127 | ||
128 | this.isUpdatingVideo = true | |
129 | ||
130 | // Update the video | |
131 | this.updateVideoAndCaptions(this.video) | |
1378c0d3 C |
132 | .subscribe({ |
133 | next: () => { | |
ce33919c | 134 | this.isUpdatingVideo = false |
66357162 | 135 | this.notifier.success($localize`Video to import updated.`) |
ce33919c | 136 | |
17119e4a | 137 | this.router.navigate([ '/my-library', 'video-imports' ]) |
ce33919c C |
138 | }, |
139 | ||
1378c0d3 | 140 | error: err => { |
7373507f C |
141 | this.error = err.message |
142 | scrollToTop() | |
42b40636 | 143 | logger.error(err) |
ce33919c | 144 | } |
1378c0d3 | 145 | }) |
ce33919c C |
146 | } |
147 | } |