]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { map, switchMap } from 'rxjs/operators' |
2e257e36 | 2 | import { AfterViewInit, Component, EventEmitter, OnInit, Output } from '@angular/core' |
fbad87b0 | 3 | import { Router } from '@angular/router' |
2e257e36 | 4 | import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core' |
67ed6552 C |
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' |
c6c0fa6c | 10 | import { hydrateFormFromVideo } from '../shared/video-edit-utils' |
66357162 | 11 | import { VideoSend } from './video-send' |
fbad87b0 C |
12 | |
13 | @Component({ | |
047559af C |
14 | selector: 'my-video-import-url', |
15 | templateUrl: './video-import-url.component.html', | |
fbad87b0 | 16 | styleUrls: [ |
78848714 | 17 | '../shared/video-edit.component.scss', |
457bb213 | 18 | './video-send.scss' |
fbad87b0 C |
19 | ] |
20 | }) | |
2e257e36 | 21 | export class VideoImportUrlComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate { |
fbad87b0 | 22 | @Output() firstStepDone = new EventEmitter<string>() |
7373507f | 23 | @Output() firstStepError = new EventEmitter<void>() |
fbad87b0 C |
24 | |
25 | targetUrl = '' | |
fbad87b0 C |
26 | |
27 | isImportingVideo = false | |
28 | hasImportedVideo = false | |
29 | isUpdatingVideo = false | |
30 | ||
fbad87b0 | 31 | video: VideoEdit |
7373507f | 32 | error: string |
fbad87b0 | 33 | |
990b6a0b | 34 | protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC |
43620009 | 35 | |
fbad87b0 C |
36 | constructor ( |
37 | protected formValidatorService: FormValidatorService, | |
43620009 | 38 | protected loadingBar: LoadingBarService, |
f8b2c1b4 | 39 | protected notifier: Notifier, |
43620009 C |
40 | protected authService: AuthService, |
41 | protected serverService: ServerService, | |
42 | protected videoService: VideoService, | |
43 | protected videoCaptionService: VideoCaptionService, | |
fbad87b0 | 44 | private router: Router, |
2e257e36 C |
45 | private videoImportService: VideoImportService, |
46 | private hooks: HooksService | |
47 | ) { | |
fbad87b0 C |
48 | super() |
49 | } | |
50 | ||
51 | ngOnInit () { | |
43620009 | 52 | super.ngOnInit() |
fbad87b0 C |
53 | } |
54 | ||
2e257e36 C |
55 | ngAfterViewInit () { |
56 | this.hooks.runAction('action:video-url-import.init', 'video-edit') | |
57 | } | |
58 | ||
fbad87b0 C |
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 = { | |
d487a997 | 71 | privacy: VideoPrivacy.PRIVATE, |
fbad87b0 C |
72 | waitTranscoding: false, |
73 | commentsEnabled: true, | |
7f2cfe3a | 74 | downloadEnabled: true, |
fbad87b0 C |
75 | channelId: this.firstStepChannelId |
76 | } | |
77 | ||
a02b93ce | 78 | this.loadingBar.useRef().start() |
5d08a6a7 | 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 }) => { | |
a02b93ce | 93 | this.loadingBar.useRef().complete() |
50ad0a1c | 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, | |
d487a997 | 111 | privacy: { id: this.firstStepPrivacyId }, |
50ad0a1c | 112 | support: null, |
b1770a0a K |
113 | thumbnailUrl, |
114 | previewUrl | |
50ad0a1c | 115 | })) |
116 | ||
117 | this.videoCaptions = videoCaptions | |
118 | ||
c6c0fa6c | 119 | hydrateFormFromVideo(this.form, this.video, true) |
50ad0a1c | 120 | }, |
121 | ||
122 | err => { | |
a02b93ce | 123 | this.loadingBar.useRef().complete() |
50ad0a1c | 124 | this.isImportingVideo = false |
125 | this.firstStepError.emit() | |
126 | this.notifier.error(err.message) | |
127 | } | |
128 | ) | |
fbad87b0 C |
129 | } |
130 | ||
131 | updateSecondStep () { | |
132 | if (this.checkForm() === false) { | |
133 | return | |
134 | } | |
135 | ||
136 | this.video.patch(this.form.value) | |
137 | ||
fbad87b0 C |
138 | this.isUpdatingVideo = true |
139 | ||
140 | // Update the video | |
43620009 | 141 | this.updateVideoAndCaptions(this.video) |
fbad87b0 C |
142 | .subscribe( |
143 | () => { | |
144 | this.isUpdatingVideo = false | |
66357162 | 145 | this.notifier.success($localize`Video to import updated.`) |
fbad87b0 | 146 | |
17119e4a | 147 | this.router.navigate([ '/my-library', 'video-imports' ]) |
fbad87b0 C |
148 | }, |
149 | ||
150 | err => { | |
7373507f C |
151 | this.error = err.message |
152 | scrollToTop() | |
fbad87b0 C |
153 | console.error(err) |
154 | } | |
155 | ) | |
fbad87b0 C |
156 | } |
157 | } |