]>
Commit | Line | Data |
---|---|---|
1 | import { Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core' | |
2 | import { Router } from '@angular/router' | |
3 | import { VideoPrivacy, VideoUpdate } from '../../../../../../shared/models/videos' | |
4 | import { AuthService, Notifier, ServerService } from '../../../core' | |
5 | import { VideoService } from '../../../shared/video/video.service' | |
6 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
7 | import { LoadingBarService } from '@ngx-loading-bar/core' | |
8 | import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send' | |
9 | import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service' | |
10 | import { VideoEdit } from '@app/shared/video/video-edit.model' | |
11 | import { FormValidatorService } from '@app/shared' | |
12 | import { VideoCaptionService } from '@app/shared/video-caption' | |
13 | import { VideoImportService } from '@app/shared/video-import' | |
14 | import { scrollToTop } from '@app/shared/misc/utils' | |
15 | ||
16 | @Component({ | |
17 | selector: 'my-video-import-torrent', | |
18 | templateUrl: './video-import-torrent.component.html', | |
19 | styleUrls: [ | |
20 | '../shared/video-edit.component.scss', | |
21 | './video-import-torrent.component.scss', | |
22 | './video-send.scss' | |
23 | ] | |
24 | }) | |
25 | export class VideoImportTorrentComponent extends VideoSend implements OnInit, CanComponentDeactivate { | |
26 | @Output() firstStepDone = new EventEmitter<string>() | |
27 | @Output() firstStepError = new EventEmitter<void>() | |
28 | @ViewChild('torrentfileInput', { static: false }) torrentfileInput: ElementRef<HTMLInputElement> | |
29 | ||
30 | magnetUri = '' | |
31 | ||
32 | isImportingVideo = false | |
33 | hasImportedVideo = false | |
34 | isUpdatingVideo = false | |
35 | ||
36 | video: VideoEdit | |
37 | error: string | |
38 | ||
39 | protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC | |
40 | ||
41 | constructor ( | |
42 | protected formValidatorService: FormValidatorService, | |
43 | protected loadingBar: LoadingBarService, | |
44 | protected notifier: Notifier, | |
45 | protected authService: AuthService, | |
46 | protected serverService: ServerService, | |
47 | protected videoService: VideoService, | |
48 | protected videoCaptionService: VideoCaptionService, | |
49 | private router: Router, | |
50 | private videoImportService: VideoImportService, | |
51 | private i18n: I18n | |
52 | ) { | |
53 | super() | |
54 | } | |
55 | ||
56 | ngOnInit () { | |
57 | super.ngOnInit() | |
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 | importVideo (torrentfile?: Blob) { | |
76 | this.isImportingVideo = true | |
77 | ||
78 | const videoUpdate: VideoUpdate = { | |
79 | privacy: this.firstStepPrivacyId, | |
80 | waitTranscoding: false, | |
81 | commentsEnabled: true, | |
82 | downloadEnabled: true, | |
83 | channelId: this.firstStepChannelId | |
84 | } | |
85 | ||
86 | this.loadingBar.start() | |
87 | ||
88 | this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate).subscribe( | |
89 | res => { | |
90 | this.loadingBar.complete() | |
91 | this.firstStepDone.emit(res.video.name) | |
92 | this.isImportingVideo = false | |
93 | this.hasImportedVideo = true | |
94 | ||
95 | this.video = new VideoEdit(Object.assign(res.video, { | |
96 | commentsEnabled: videoUpdate.commentsEnabled, | |
97 | downloadEnabled: videoUpdate.downloadEnabled, | |
98 | support: null, | |
99 | thumbnailUrl: null, | |
100 | previewUrl: null | |
101 | })) | |
102 | ||
103 | this.hydrateFormFromVideo() | |
104 | }, | |
105 | ||
106 | err => { | |
107 | this.loadingBar.complete() | |
108 | this.isImportingVideo = false | |
109 | this.firstStepError.emit() | |
110 | this.notifier.error(err.message) | |
111 | } | |
112 | ) | |
113 | } | |
114 | ||
115 | updateSecondStep () { | |
116 | if (this.checkForm() === false) { | |
117 | return | |
118 | } | |
119 | ||
120 | this.video.patch(this.form.value) | |
121 | ||
122 | this.isUpdatingVideo = true | |
123 | ||
124 | // Update the video | |
125 | this.updateVideoAndCaptions(this.video) | |
126 | .subscribe( | |
127 | () => { | |
128 | this.isUpdatingVideo = false | |
129 | this.notifier.success(this.i18n('Video to import updated.')) | |
130 | ||
131 | this.router.navigate([ '/my-account', 'video-imports' ]) | |
132 | }, | |
133 | ||
134 | err => { | |
135 | this.error = err.message | |
136 | scrollToTop() | |
137 | console.error(err) | |
138 | } | |
139 | ) | |
140 | ||
141 | } | |
142 | ||
143 | private hydrateFormFromVideo () { | |
144 | this.form.patchValue(this.video.toFormPatch()) | |
145 | } | |
146 | } |