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