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