]>
Commit | Line | Data |
---|---|---|
0146f351 | 1 | import { forkJoin } from 'rxjs' |
67ed6552 | 2 | import { map, switchMap } from 'rxjs/operators' |
2e257e36 | 3 | import { AfterViewInit, Component, EventEmitter, OnInit, Output } from '@angular/core' |
fbad87b0 | 4 | import { Router } from '@angular/router' |
2e257e36 | 5 | import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core' |
0146f351 | 6 | import { scrollToTop } from '@app/helpers' |
67ed6552 C |
7 | import { FormValidatorService } from '@app/shared/shared-forms' |
8 | import { VideoCaptionService, VideoEdit, VideoImportService, VideoService } from '@app/shared/shared-main' | |
67ed6552 | 9 | import { LoadingBarService } from '@ngx-loading-bar/core' |
1378c0d3 | 10 | import { VideoUpdate } from '@shared/models' |
c6c0fa6c | 11 | import { hydrateFormFromVideo } from '../shared/video-edit-utils' |
66357162 | 12 | import { VideoSend } from './video-send' |
fbad87b0 C |
13 | |
14 | @Component({ | |
047559af C |
15 | selector: 'my-video-import-url', |
16 | templateUrl: './video-import-url.component.html', | |
fbad87b0 | 17 | styleUrls: [ |
78848714 | 18 | '../shared/video-edit.component.scss', |
457bb213 | 19 | './video-send.scss' |
fbad87b0 C |
20 | ] |
21 | }) | |
2e257e36 | 22 | export class VideoImportUrlComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate { |
fbad87b0 | 23 | @Output() firstStepDone = new EventEmitter<string>() |
7373507f | 24 | @Output() firstStepError = new EventEmitter<void>() |
fbad87b0 C |
25 | |
26 | targetUrl = '' | |
fbad87b0 C |
27 | |
28 | isImportingVideo = false | |
29 | hasImportedVideo = false | |
30 | isUpdatingVideo = false | |
31 | ||
fbad87b0 | 32 | video: VideoEdit |
7373507f | 33 | error: string |
fbad87b0 C |
34 | |
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, |
2e257e36 C |
44 | private videoImportService: VideoImportService, |
45 | private hooks: HooksService | |
46 | ) { | |
fbad87b0 C |
47 | super() |
48 | } | |
49 | ||
50 | ngOnInit () { | |
43620009 | 51 | super.ngOnInit() |
fbad87b0 C |
52 | } |
53 | ||
2e257e36 C |
54 | ngAfterViewInit () { |
55 | this.hooks.runAction('action:video-url-import.init', 'video-edit') | |
56 | } | |
57 | ||
fbad87b0 C |
58 | canDeactivate () { |
59 | return { canDeactivate: true } | |
60 | } | |
61 | ||
fbad87b0 | 62 | isTargetUrlValid () { |
9df52d66 | 63 | return this.targetUrl?.match(/https?:\/\//) |
fbad87b0 C |
64 | } |
65 | ||
66 | importVideo () { | |
67 | this.isImportingVideo = true | |
68 | ||
69 | const videoUpdate: VideoUpdate = { | |
a3f45a2a | 70 | privacy: this.highestPrivacy, |
fbad87b0 | 71 | waitTranscoding: false, |
fbad87b0 C |
72 | channelId: this.firstStepChannelId |
73 | } | |
74 | ||
a02b93ce | 75 | this.loadingBar.useRef().start() |
5d08a6a7 | 76 | |
50ad0a1c | 77 | this.videoImportService |
78 | .importVideoUrl(this.targetUrl, videoUpdate) | |
79 | .pipe( | |
0146f351 C |
80 | switchMap(previous => { |
81 | return forkJoin([ | |
82 | this.videoCaptionService.listCaptions(previous.video.uuid), | |
83 | this.videoService.getVideo({ videoId: previous.video.uuid }) | |
84 | ]).pipe(map(([ videoCaptionsResult, video ]) => ({ videoCaptions: videoCaptionsResult.data, video }))) | |
50ad0a1c | 85 | }) |
86 | ) | |
1378c0d3 C |
87 | .subscribe({ |
88 | next: ({ video, videoCaptions }) => { | |
a02b93ce | 89 | this.loadingBar.useRef().complete() |
50ad0a1c | 90 | this.firstStepDone.emit(video.name) |
91 | this.isImportingVideo = false | |
92 | this.hasImportedVideo = true | |
93 | ||
0146f351 C |
94 | this.video = new VideoEdit(video) |
95 | this.video.patch({ privacy: this.firstStepPrivacyId }) | |
50ad0a1c | 96 | |
97 | this.videoCaptions = videoCaptions | |
98 | ||
c6c0fa6c | 99 | hydrateFormFromVideo(this.form, this.video, true) |
50ad0a1c | 100 | }, |
101 | ||
1378c0d3 | 102 | error: err => { |
a02b93ce | 103 | this.loadingBar.useRef().complete() |
50ad0a1c | 104 | this.isImportingVideo = false |
105 | this.firstStepError.emit() | |
106 | this.notifier.error(err.message) | |
107 | } | |
1378c0d3 | 108 | }) |
fbad87b0 C |
109 | } |
110 | ||
cc4bf76c C |
111 | async updateSecondStep () { |
112 | if (!await this.isFormValid()) return | |
fbad87b0 C |
113 | |
114 | this.video.patch(this.form.value) | |
115 | ||
fbad87b0 C |
116 | this.isUpdatingVideo = true |
117 | ||
118 | // Update the video | |
43620009 | 119 | this.updateVideoAndCaptions(this.video) |
1378c0d3 C |
120 | .subscribe({ |
121 | next: () => { | |
fbad87b0 | 122 | this.isUpdatingVideo = false |
66357162 | 123 | this.notifier.success($localize`Video to import updated.`) |
fbad87b0 | 124 | |
17119e4a | 125 | this.router.navigate([ '/my-library', 'video-imports' ]) |
fbad87b0 C |
126 | }, |
127 | ||
1378c0d3 | 128 | error: err => { |
7373507f C |
129 | this.error = err.message |
130 | scrollToTop() | |
fbad87b0 C |
131 | console.error(err) |
132 | } | |
1378c0d3 | 133 | }) |
fbad87b0 C |
134 | } |
135 | } |