]>
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' |
1378c0d3 | 9 | import { 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 C |
33 | |
34 | constructor ( | |
35 | protected formValidatorService: FormValidatorService, | |
43620009 | 36 | protected loadingBar: LoadingBarService, |
f8b2c1b4 | 37 | protected notifier: Notifier, |
43620009 C |
38 | protected authService: AuthService, |
39 | protected serverService: ServerService, | |
40 | protected videoService: VideoService, | |
41 | protected videoCaptionService: VideoCaptionService, | |
fbad87b0 | 42 | private router: Router, |
2e257e36 C |
43 | private videoImportService: VideoImportService, |
44 | private hooks: HooksService | |
45 | ) { | |
fbad87b0 C |
46 | super() |
47 | } | |
48 | ||
49 | ngOnInit () { | |
43620009 | 50 | super.ngOnInit() |
fbad87b0 C |
51 | } |
52 | ||
2e257e36 C |
53 | ngAfterViewInit () { |
54 | this.hooks.runAction('action:video-url-import.init', 'video-edit') | |
55 | } | |
56 | ||
fbad87b0 C |
57 | canDeactivate () { |
58 | return { canDeactivate: true } | |
59 | } | |
60 | ||
fbad87b0 | 61 | isTargetUrlValid () { |
9df52d66 | 62 | return this.targetUrl?.match(/https?:\/\//) |
fbad87b0 C |
63 | } |
64 | ||
65 | importVideo () { | |
66 | this.isImportingVideo = true | |
67 | ||
68 | const videoUpdate: VideoUpdate = { | |
a3f45a2a | 69 | privacy: this.highestPrivacy, |
fbad87b0 C |
70 | waitTranscoding: false, |
71 | commentsEnabled: true, | |
7f2cfe3a | 72 | downloadEnabled: true, |
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( | |
81 | switchMap(res => { | |
82 | return this.videoCaptionService | |
83 | .listCaptions(res.video.id) | |
84 | .pipe( | |
85 | map(result => ({ video: res.video, videoCaptions: result.data })) | |
86 | ) | |
87 | }) | |
88 | ) | |
1378c0d3 C |
89 | .subscribe({ |
90 | next: ({ video, videoCaptions }) => { | |
a02b93ce | 91 | this.loadingBar.useRef().complete() |
50ad0a1c | 92 | this.firstStepDone.emit(video.name) |
93 | this.isImportingVideo = false | |
94 | this.hasImportedVideo = true | |
95 | ||
b1770a0a K |
96 | const absoluteAPIUrl = getAbsoluteAPIUrl() |
97 | ||
98 | const thumbnailUrl = video.thumbnailPath | |
99 | ? absoluteAPIUrl + video.thumbnailPath | |
100 | : null | |
101 | ||
102 | const previewUrl = video.previewPath | |
103 | ? absoluteAPIUrl + video.previewPath | |
104 | : null | |
105 | ||
50ad0a1c | 106 | this.video = new VideoEdit(Object.assign(video, { |
107 | commentsEnabled: videoUpdate.commentsEnabled, | |
108 | downloadEnabled: videoUpdate.downloadEnabled, | |
d487a997 | 109 | privacy: { id: this.firstStepPrivacyId }, |
50ad0a1c | 110 | support: null, |
b1770a0a K |
111 | thumbnailUrl, |
112 | previewUrl | |
50ad0a1c | 113 | })) |
114 | ||
115 | this.videoCaptions = videoCaptions | |
116 | ||
c6c0fa6c | 117 | hydrateFormFromVideo(this.form, this.video, true) |
50ad0a1c | 118 | }, |
119 | ||
1378c0d3 | 120 | error: err => { |
a02b93ce | 121 | this.loadingBar.useRef().complete() |
50ad0a1c | 122 | this.isImportingVideo = false |
123 | this.firstStepError.emit() | |
124 | this.notifier.error(err.message) | |
125 | } | |
1378c0d3 | 126 | }) |
fbad87b0 C |
127 | } |
128 | ||
129 | updateSecondStep () { | |
130 | if (this.checkForm() === false) { | |
131 | return | |
132 | } | |
133 | ||
134 | this.video.patch(this.form.value) | |
135 | ||
fbad87b0 C |
136 | this.isUpdatingVideo = true |
137 | ||
138 | // Update the video | |
43620009 | 139 | this.updateVideoAndCaptions(this.video) |
1378c0d3 C |
140 | .subscribe({ |
141 | next: () => { | |
fbad87b0 | 142 | this.isUpdatingVideo = false |
66357162 | 143 | this.notifier.success($localize`Video to import updated.`) |
fbad87b0 | 144 | |
17119e4a | 145 | this.router.navigate([ '/my-library', 'video-imports' ]) |
fbad87b0 C |
146 | }, |
147 | ||
1378c0d3 | 148 | error: err => { |
7373507f C |
149 | this.error = err.message |
150 | scrollToTop() | |
fbad87b0 C |
151 | console.error(err) |
152 | } | |
1378c0d3 | 153 | }) |
fbad87b0 C |
154 | } |
155 | } |