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