]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-edit/video-add-components/video-import-url.component.ts
Add client action hooks for pubish pages
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-add-components / video-import-url.component.ts
1 import { map, switchMap } from 'rxjs/operators'
2 import { AfterViewInit, Component, EventEmitter, OnInit, Output } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core'
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'
8 import { LoadingBarService } from '@ngx-loading-bar/core'
9 import { VideoPrivacy, VideoUpdate } from '@shared/models'
10 import { hydrateFormFromVideo } from '../shared/video-edit-utils'
11 import { VideoSend } from './video-send'
12
13 @Component({
14 selector: 'my-video-import-url',
15 templateUrl: './video-import-url.component.html',
16 styleUrls: [
17 '../shared/video-edit.component.scss',
18 './video-send.scss'
19 ]
20 })
21 export class VideoImportUrlComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
22 @Output() firstStepDone = new EventEmitter<string>()
23 @Output() firstStepError = new EventEmitter<void>()
24
25 targetUrl = ''
26
27 isImportingVideo = false
28 hasImportedVideo = false
29 isUpdatingVideo = false
30
31 video: VideoEdit
32 error: string
33
34 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
35
36 constructor (
37 protected formValidatorService: FormValidatorService,
38 protected loadingBar: LoadingBarService,
39 protected notifier: Notifier,
40 protected authService: AuthService,
41 protected serverService: ServerService,
42 protected videoService: VideoService,
43 protected videoCaptionService: VideoCaptionService,
44 private router: Router,
45 private videoImportService: VideoImportService,
46 private hooks: HooksService
47 ) {
48 super()
49 }
50
51 ngOnInit () {
52 super.ngOnInit()
53 }
54
55 ngAfterViewInit () {
56 this.hooks.runAction('action:video-url-import.init', 'video-edit')
57 }
58
59 canDeactivate () {
60 return { canDeactivate: true }
61 }
62
63 isTargetUrlValid () {
64 return this.targetUrl && this.targetUrl.match(/https?:\/\//)
65 }
66
67 importVideo () {
68 this.isImportingVideo = true
69
70 const videoUpdate: VideoUpdate = {
71 privacy: VideoPrivacy.PRIVATE,
72 waitTranscoding: false,
73 commentsEnabled: true,
74 downloadEnabled: true,
75 channelId: this.firstStepChannelId
76 }
77
78 this.loadingBar.useRef().start()
79
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 }) => {
93 this.loadingBar.useRef().complete()
94 this.firstStepDone.emit(video.name)
95 this.isImportingVideo = false
96 this.hasImportedVideo = true
97
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
108 this.video = new VideoEdit(Object.assign(video, {
109 commentsEnabled: videoUpdate.commentsEnabled,
110 downloadEnabled: videoUpdate.downloadEnabled,
111 privacy: { id: this.firstStepPrivacyId },
112 support: null,
113 thumbnailUrl,
114 previewUrl
115 }))
116
117 this.videoCaptions = videoCaptions
118
119 hydrateFormFromVideo(this.form, this.video, true)
120 },
121
122 err => {
123 this.loadingBar.useRef().complete()
124 this.isImportingVideo = false
125 this.firstStepError.emit()
126 this.notifier.error(err.message)
127 }
128 )
129 }
130
131 updateSecondStep () {
132 if (this.checkForm() === false) {
133 return
134 }
135
136 this.video.patch(this.form.value)
137
138 this.isUpdatingVideo = true
139
140 // Update the video
141 this.updateVideoAndCaptions(this.video)
142 .subscribe(
143 () => {
144 this.isUpdatingVideo = false
145 this.notifier.success($localize`Video to import updated.`)
146
147 this.router.navigate([ '/my-library', 'video-imports' ])
148 },
149
150 err => {
151 this.error = err.message
152 scrollToTop()
153 console.error(err)
154 }
155 )
156 }
157 }