]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-import.component.ts
Add import http enabled configuration
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-import.component.ts
1 import { Component, EventEmitter, OnInit, Output } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
4 import { NotificationsService } from 'angular2-notifications'
5 import { VideoConstant, VideoPrivacy, VideoUpdate } from '../../../../../shared/models/videos'
6 import { AuthService, ServerService } from '../../core'
7 import { FormReactive } from '../../shared'
8 import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
9 import { VideoService } from '../../shared/video/video.service'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
12 import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
13 import { VideoImportService } from '@app/shared/video-import'
14 import { VideoEdit } from '@app/shared/video/video-edit.model'
15 import { switchMap } from 'rxjs/operators'
16 import { LoadingBarService } from '@ngx-loading-bar/core'
17 import { VideoCaptionService } from '@app/shared/video-caption'
18
19 @Component({
20 selector: 'my-video-import',
21 templateUrl: './video-import.component.html',
22 styleUrls: [
23 './shared/video-edit.component.scss',
24 './video-import.component.scss'
25 ]
26 })
27 export class VideoImportComponent extends FormReactive implements OnInit, CanComponentDeactivate {
28 @Output() firstStepDone = new EventEmitter<string>()
29
30 targetUrl = ''
31 videoFileName: string
32
33 isImportingVideo = false
34 hasImportedVideo = false
35 isUpdatingVideo = false
36
37 userVideoChannels: { id: number, label: string, support: string }[] = []
38 videoPrivacies: VideoConstant<string>[] = []
39 videoCaptions: VideoCaptionEdit[] = []
40
41 firstStepPrivacyId = 0
42 firstStepChannelId = 0
43 video: VideoEdit
44
45 constructor (
46 protected formValidatorService: FormValidatorService,
47 private router: Router,
48 private loadingBar: LoadingBarService,
49 private notificationsService: NotificationsService,
50 private authService: AuthService,
51 private serverService: ServerService,
52 private videoService: VideoService,
53 private videoImportService: VideoImportService,
54 private videoCaptionService: VideoCaptionService,
55 private i18n: I18n
56 ) {
57 super()
58 }
59
60 ngOnInit () {
61 this.buildForm({})
62
63 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
64 .then(() => this.firstStepChannelId = this.userVideoChannels[ 0 ].id)
65
66 this.serverService.videoPrivaciesLoaded
67 .subscribe(
68 () => {
69 this.videoPrivacies = this.serverService.getVideoPrivacies()
70
71 // Private by default
72 this.firstStepPrivacyId = VideoPrivacy.PRIVATE
73 })
74 }
75
76 canDeactivate () {
77 return { canDeactivate: true }
78 }
79
80 checkForm () {
81 this.forceCheck()
82
83 return this.form.valid
84 }
85
86 isTargetUrlValid () {
87 return this.targetUrl && this.targetUrl.match(/https?:\/\//)
88 }
89
90 importVideo () {
91 this.isImportingVideo = true
92
93 const videoUpdate: VideoUpdate = {
94 privacy: this.firstStepPrivacyId,
95 waitTranscoding: false,
96 commentsEnabled: true,
97 channelId: this.firstStepChannelId
98 }
99
100 this.loadingBar.start()
101
102 this.videoImportService.importVideo(this.targetUrl, videoUpdate).subscribe(
103 res => {
104 this.loadingBar.complete()
105 this.firstStepDone.emit(res.video.name)
106 this.isImportingVideo = false
107 this.hasImportedVideo = true
108
109 this.video = new VideoEdit(Object.assign(res.video, {
110 commentsEnabled: videoUpdate.commentsEnabled,
111 support: null,
112 thumbnailUrl: null,
113 previewUrl: null
114 }))
115 this.hydrateFormFromVideo()
116 },
117
118 err => {
119 this.loadingBar.complete()
120 this.isImportingVideo = false
121 this.notificationsService.error(this.i18n('Error'), err.message)
122 }
123 )
124 }
125
126 updateSecondStep () {
127 if (this.checkForm() === false) {
128 return
129 }
130
131 this.video.patch(this.form.value)
132
133 this.loadingBar.start()
134 this.isUpdatingVideo = true
135
136 // Update the video
137 this.videoService.updateVideo(this.video)
138 .pipe(
139 // Then update captions
140 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
141 )
142 .subscribe(
143 () => {
144 this.isUpdatingVideo = false
145 this.loadingBar.complete()
146 this.notificationsService.success(this.i18n('Success'), this.i18n('Video to import updated.'))
147
148 // TODO: route to imports list
149 // this.router.navigate([ '/videos/watch', this.video.uuid ])
150 },
151
152 err => {
153 this.loadingBar.complete()
154 this.isUpdatingVideo = false
155 this.notificationsService.error(this.i18n('Error'), err.message)
156 console.error(err)
157 }
158 )
159
160 }
161
162 private hydrateFormFromVideo () {
163 this.form.patchValue(this.video.toFormPatch())
164 }
165 }