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