]>
Commit | Line | Data |
---|---|---|
1 | import { AfterViewInit, Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core' | |
2 | import { Router } from '@angular/router' | |
3 | import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core' | |
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' | |
7 | import { LoadingBarService } from '@ngx-loading-bar/core' | |
8 | import { VideoPrivacy, VideoUpdate } from '@shared/models' | |
9 | import { hydrateFormFromVideo } from '../shared/video-edit-utils' | |
10 | import { VideoSend } from './video-send' | |
11 | ||
12 | @Component({ | |
13 | selector: 'my-video-import-torrent', | |
14 | templateUrl: './video-import-torrent.component.html', | |
15 | styleUrls: [ | |
16 | '../shared/video-edit.component.scss', | |
17 | './video-import-torrent.component.scss', | |
18 | './video-send.scss' | |
19 | ] | |
20 | }) | |
21 | export class VideoImportTorrentComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate { | |
22 | @Output() firstStepDone = new EventEmitter<string>() | |
23 | @Output() firstStepError = new EventEmitter<void>() | |
24 | @ViewChild('torrentfileInput') torrentfileInput: ElementRef<HTMLInputElement> | |
25 | ||
26 | magnetUri = '' | |
27 | ||
28 | isImportingVideo = false | |
29 | hasImportedVideo = false | |
30 | isUpdatingVideo = false | |
31 | ||
32 | video: VideoEdit | |
33 | error: string | |
34 | ||
35 | protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC | |
36 | ||
37 | constructor ( | |
38 | protected formValidatorService: FormValidatorService, | |
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: VideoPrivacy.PRIVATE, | |
85 | waitTranscoding: false, | |
86 | commentsEnabled: true, | |
87 | downloadEnabled: true, | |
88 | channelId: this.firstStepChannelId | |
89 | } | |
90 | ||
91 | this.loadingBar.useRef().start() | |
92 | ||
93 | this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate).subscribe( | |
94 | res => { | |
95 | this.loadingBar.useRef().complete() | |
96 | this.firstStepDone.emit(res.video.name) | |
97 | this.isImportingVideo = false | |
98 | this.hasImportedVideo = true | |
99 | ||
100 | this.video = new VideoEdit(Object.assign(res.video, { | |
101 | commentsEnabled: videoUpdate.commentsEnabled, | |
102 | downloadEnabled: videoUpdate.downloadEnabled, | |
103 | privacy: { id: this.firstStepPrivacyId }, | |
104 | support: null, | |
105 | thumbnailUrl: null, | |
106 | previewUrl: null | |
107 | })) | |
108 | ||
109 | hydrateFormFromVideo(this.form, this.video, false) | |
110 | }, | |
111 | ||
112 | err => { | |
113 | this.loadingBar.useRef().complete() | |
114 | this.isImportingVideo = false | |
115 | this.firstStepError.emit() | |
116 | this.notifier.error(err.message) | |
117 | } | |
118 | ) | |
119 | } | |
120 | ||
121 | updateSecondStep () { | |
122 | if (this.checkForm() === false) { | |
123 | return | |
124 | } | |
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 | () => { | |
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 | err => { | |
141 | this.error = err.message | |
142 | scrollToTop() | |
143 | console.error(err) | |
144 | } | |
145 | ) | |
146 | } | |
147 | } |