]>
Commit | Line | Data |
---|---|---|
fbad87b0 C |
1 | import { Component, EventEmitter, OnInit, Output } from '@angular/core' |
2 | import { Router } from '@angular/router' | |
fbad87b0 | 3 | import { NotificationsService } from 'angular2-notifications' |
78848714 C |
4 | import { VideoPrivacy, VideoUpdate } from '../../../../../../shared/models/videos' |
5 | import { AuthService, ServerService } from '../../../core' | |
6 | import { VideoService } from '../../../shared/video/video.service' | |
fbad87b0 | 7 | import { I18n } from '@ngx-translate/i18n-polyfill' |
fbad87b0 | 8 | import { LoadingBarService } from '@ngx-loading-bar/core' |
78848714 C |
9 | import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send' |
10 | import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service' | |
11 | import { VideoEdit } from '@app/shared/video/video-edit.model' | |
12 | import { FormValidatorService } from '@app/shared' | |
fbad87b0 | 13 | import { VideoCaptionService } from '@app/shared/video-caption' |
78848714 | 14 | import { VideoImportService } from '@app/shared/video-import' |
fbad87b0 C |
15 | |
16 | @Component({ | |
047559af C |
17 | selector: 'my-video-import-url', |
18 | templateUrl: './video-import-url.component.html', | |
fbad87b0 | 19 | styleUrls: [ |
78848714 | 20 | '../shared/video-edit.component.scss', |
047559af | 21 | './video-import-url.component.scss' |
fbad87b0 C |
22 | ] |
23 | }) | |
047559af | 24 | export class VideoImportUrlComponent extends VideoSend implements OnInit, CanComponentDeactivate { |
fbad87b0 C |
25 | @Output() firstStepDone = new EventEmitter<string>() |
26 | ||
27 | targetUrl = '' | |
28 | videoFileName: string | |
29 | ||
30 | isImportingVideo = false | |
31 | hasImportedVideo = false | |
32 | isUpdatingVideo = false | |
33 | ||
fbad87b0 C |
34 | video: VideoEdit |
35 | ||
990b6a0b | 36 | protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC |
43620009 | 37 | |
fbad87b0 C |
38 | constructor ( |
39 | protected formValidatorService: FormValidatorService, | |
43620009 C |
40 | protected loadingBar: LoadingBarService, |
41 | protected notificationsService: NotificationsService, | |
42 | protected authService: AuthService, | |
43 | protected serverService: ServerService, | |
44 | protected videoService: VideoService, | |
45 | protected videoCaptionService: VideoCaptionService, | |
fbad87b0 | 46 | private router: Router, |
fbad87b0 | 47 | private videoImportService: VideoImportService, |
fbad87b0 C |
48 | private i18n: I18n |
49 | ) { | |
50 | super() | |
51 | } | |
52 | ||
53 | ngOnInit () { | |
43620009 | 54 | super.ngOnInit() |
fbad87b0 C |
55 | } |
56 | ||
57 | canDeactivate () { | |
58 | return { canDeactivate: true } | |
59 | } | |
60 | ||
fbad87b0 C |
61 | isTargetUrlValid () { |
62 | return this.targetUrl && this.targetUrl.match(/https?:\/\//) | |
63 | } | |
64 | ||
65 | importVideo () { | |
66 | this.isImportingVideo = true | |
67 | ||
68 | const videoUpdate: VideoUpdate = { | |
69 | privacy: this.firstStepPrivacyId, | |
70 | waitTranscoding: false, | |
71 | commentsEnabled: true, | |
72 | channelId: this.firstStepChannelId | |
73 | } | |
74 | ||
5d08a6a7 C |
75 | this.loadingBar.start() |
76 | ||
ce33919c | 77 | this.videoImportService.importVideoUrl(this.targetUrl, videoUpdate).subscribe( |
fbad87b0 | 78 | res => { |
5d08a6a7 | 79 | this.loadingBar.complete() |
fbad87b0 C |
80 | this.firstStepDone.emit(res.video.name) |
81 | this.isImportingVideo = false | |
82 | this.hasImportedVideo = true | |
83 | ||
84 | this.video = new VideoEdit(Object.assign(res.video, { | |
85 | commentsEnabled: videoUpdate.commentsEnabled, | |
86 | support: null, | |
87 | thumbnailUrl: null, | |
88 | previewUrl: null | |
89 | })) | |
8cd7faaa C |
90 | |
91 | this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies) | |
92 | ||
fbad87b0 C |
93 | this.hydrateFormFromVideo() |
94 | }, | |
95 | ||
96 | err => { | |
5d08a6a7 | 97 | this.loadingBar.complete() |
fbad87b0 C |
98 | this.isImportingVideo = false |
99 | this.notificationsService.error(this.i18n('Error'), err.message) | |
100 | } | |
101 | ) | |
102 | } | |
103 | ||
104 | updateSecondStep () { | |
105 | if (this.checkForm() === false) { | |
106 | return | |
107 | } | |
108 | ||
109 | this.video.patch(this.form.value) | |
110 | ||
fbad87b0 C |
111 | this.isUpdatingVideo = true |
112 | ||
113 | // Update the video | |
43620009 | 114 | this.updateVideoAndCaptions(this.video) |
fbad87b0 C |
115 | .subscribe( |
116 | () => { | |
117 | this.isUpdatingVideo = false | |
fbad87b0 C |
118 | this.notificationsService.success(this.i18n('Success'), this.i18n('Video to import updated.')) |
119 | ||
b2977eec | 120 | this.router.navigate([ '/my-account', 'video-imports' ]) |
fbad87b0 C |
121 | }, |
122 | ||
123 | err => { | |
fbad87b0 C |
124 | this.isUpdatingVideo = false |
125 | this.notificationsService.error(this.i18n('Error'), err.message) | |
126 | console.error(err) | |
127 | } | |
128 | ) | |
129 | ||
130 | } | |
131 | ||
132 | private hydrateFormFromVideo () { | |
133 | this.form.patchValue(this.video.toFormPatch()) | |
134 | } | |
135 | } |