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