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