]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { map, switchMap } from 'rxjs/operators' |
fbad87b0 C |
2 | import { Component, EventEmitter, OnInit, Output } from '@angular/core' |
3 | import { Router } from '@angular/router' | |
67ed6552 C |
4 | import { AuthService, CanComponentDeactivate, Notifier, ServerService } from '@app/core' |
5 | import { getAbsoluteAPIUrl, 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' |
67ed6552 | 9 | import { VideoPrivacy, VideoUpdate } from '@shared/models' |
66357162 | 10 | import { VideoSend } from './video-send' |
fbad87b0 C |
11 | |
12 | @Component({ | |
047559af C |
13 | selector: 'my-video-import-url', |
14 | templateUrl: './video-import-url.component.html', | |
fbad87b0 | 15 | styleUrls: [ |
78848714 | 16 | '../shared/video-edit.component.scss', |
457bb213 | 17 | './video-send.scss' |
fbad87b0 C |
18 | ] |
19 | }) | |
047559af | 20 | export class VideoImportUrlComponent extends VideoSend implements OnInit, CanComponentDeactivate { |
fbad87b0 | 21 | @Output() firstStepDone = new EventEmitter<string>() |
7373507f | 22 | @Output() firstStepError = new EventEmitter<void>() |
fbad87b0 C |
23 | |
24 | targetUrl = '' | |
fbad87b0 C |
25 | |
26 | isImportingVideo = false | |
27 | hasImportedVideo = false | |
28 | isUpdatingVideo = false | |
29 | ||
fbad87b0 | 30 | video: VideoEdit |
7373507f | 31 | error: string |
fbad87b0 | 32 | |
990b6a0b | 33 | protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC |
43620009 | 34 | |
fbad87b0 C |
35 | constructor ( |
36 | protected formValidatorService: FormValidatorService, | |
43620009 | 37 | protected loadingBar: LoadingBarService, |
f8b2c1b4 | 38 | protected notifier: Notifier, |
43620009 C |
39 | protected authService: AuthService, |
40 | protected serverService: ServerService, | |
41 | protected videoService: VideoService, | |
42 | protected videoCaptionService: VideoCaptionService, | |
fbad87b0 | 43 | private router: Router, |
66357162 C |
44 | private videoImportService: VideoImportService |
45 | ) { | |
fbad87b0 C |
46 | super() |
47 | } | |
48 | ||
49 | ngOnInit () { | |
43620009 | 50 | super.ngOnInit() |
fbad87b0 C |
51 | } |
52 | ||
53 | canDeactivate () { | |
54 | return { canDeactivate: true } | |
55 | } | |
56 | ||
fbad87b0 C |
57 | isTargetUrlValid () { |
58 | return this.targetUrl && this.targetUrl.match(/https?:\/\//) | |
59 | } | |
60 | ||
61 | importVideo () { | |
62 | this.isImportingVideo = true | |
63 | ||
64 | const videoUpdate: VideoUpdate = { | |
65 | privacy: this.firstStepPrivacyId, | |
66 | waitTranscoding: false, | |
67 | commentsEnabled: true, | |
7f2cfe3a | 68 | downloadEnabled: true, |
fbad87b0 C |
69 | channelId: this.firstStepChannelId |
70 | } | |
71 | ||
a02b93ce | 72 | this.loadingBar.useRef().start() |
5d08a6a7 | 73 | |
50ad0a1c | 74 | this.videoImportService |
75 | .importVideoUrl(this.targetUrl, videoUpdate) | |
76 | .pipe( | |
77 | switchMap(res => { | |
78 | return this.videoCaptionService | |
79 | .listCaptions(res.video.id) | |
80 | .pipe( | |
81 | map(result => ({ video: res.video, videoCaptions: result.data })) | |
82 | ) | |
83 | }) | |
84 | ) | |
85 | .subscribe( | |
86 | ({ video, videoCaptions }) => { | |
a02b93ce | 87 | this.loadingBar.useRef().complete() |
50ad0a1c | 88 | this.firstStepDone.emit(video.name) |
89 | this.isImportingVideo = false | |
90 | this.hasImportedVideo = true | |
91 | ||
b1770a0a K |
92 | const absoluteAPIUrl = getAbsoluteAPIUrl() |
93 | ||
94 | const thumbnailUrl = video.thumbnailPath | |
95 | ? absoluteAPIUrl + video.thumbnailPath | |
96 | : null | |
97 | ||
98 | const previewUrl = video.previewPath | |
99 | ? absoluteAPIUrl + video.previewPath | |
100 | : null | |
101 | ||
50ad0a1c | 102 | this.video = new VideoEdit(Object.assign(video, { |
103 | commentsEnabled: videoUpdate.commentsEnabled, | |
104 | downloadEnabled: videoUpdate.downloadEnabled, | |
105 | support: null, | |
b1770a0a K |
106 | thumbnailUrl, |
107 | previewUrl | |
50ad0a1c | 108 | })) |
109 | ||
110 | this.videoCaptions = videoCaptions | |
111 | ||
112 | this.hydrateFormFromVideo() | |
113 | }, | |
114 | ||
115 | err => { | |
a02b93ce | 116 | this.loadingBar.useRef().complete() |
50ad0a1c | 117 | this.isImportingVideo = false |
118 | this.firstStepError.emit() | |
119 | this.notifier.error(err.message) | |
120 | } | |
121 | ) | |
fbad87b0 C |
122 | } |
123 | ||
124 | updateSecondStep () { | |
125 | if (this.checkForm() === false) { | |
126 | return | |
127 | } | |
128 | ||
129 | this.video.patch(this.form.value) | |
130 | ||
fbad87b0 C |
131 | this.isUpdatingVideo = true |
132 | ||
133 | // Update the video | |
43620009 | 134 | this.updateVideoAndCaptions(this.video) |
fbad87b0 C |
135 | .subscribe( |
136 | () => { | |
137 | this.isUpdatingVideo = false | |
66357162 | 138 | this.notifier.success($localize`Video to import updated.`) |
fbad87b0 | 139 | |
b2977eec | 140 | this.router.navigate([ '/my-account', 'video-imports' ]) |
fbad87b0 C |
141 | }, |
142 | ||
143 | err => { | |
7373507f C |
144 | this.error = err.message |
145 | scrollToTop() | |
fbad87b0 C |
146 | console.error(err) |
147 | } | |
148 | ) | |
149 | ||
150 | } | |
151 | ||
152 | private hydrateFormFromVideo () { | |
153 | this.form.patchValue(this.video.toFormPatch()) | |
b1770a0a K |
154 | |
155 | const objects = [ | |
156 | { | |
157 | url: 'thumbnailUrl', | |
158 | name: 'thumbnailfile' | |
159 | }, | |
160 | { | |
161 | url: 'previewUrl', | |
162 | name: 'previewfile' | |
163 | } | |
164 | ] | |
165 | ||
166 | for (const obj of objects) { | |
167 | fetch(this.video[obj.url]) | |
168 | .then(response => response.blob()) | |
169 | .then(data => { | |
170 | this.form.patchValue({ | |
171 | [ obj.name ]: data | |
172 | }) | |
173 | }) | |
174 | } | |
fbad87b0 C |
175 | } |
176 | } |