]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-add-components/video-import-url.component.ts
Merge branch 'develop' of https://github.com/Chocobozzz/PeerTube into move-utils...
[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 { NotificationsService } from 'angular2-notifications'
4 import { VideoPrivacy, VideoUpdate } from '../../../../../../shared/models/videos'
5 import { AuthService, ServerService } from '../../../core'
6 import { VideoService } from '../../../shared/video/video.service'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { LoadingBarService } from '@ngx-loading-bar/core'
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'
13 import { VideoCaptionService } from '@app/shared/video-caption'
14 import { VideoImportService } from '@app/shared/video-import'
15 import { scrollToTop } from '@app/shared/misc/utils'
16
17 @Component({
18 selector: 'my-video-import-url',
19 templateUrl: './video-import-url.component.html',
20 styleUrls: [
21 '../shared/video-edit.component.scss',
22 './video-import-url.component.scss'
23 ]
24 })
25 export class VideoImportUrlComponent extends VideoSend implements OnInit, CanComponentDeactivate {
26 @Output() firstStepDone = new EventEmitter<string>()
27 @Output() firstStepError = new EventEmitter<void>()
28
29 targetUrl = ''
30
31 isImportingVideo = false
32 hasImportedVideo = false
33 isUpdatingVideo = false
34
35 video: VideoEdit
36 error: string
37
38 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
39
40 constructor (
41 protected formValidatorService: FormValidatorService,
42 protected loadingBar: LoadingBarService,
43 protected notificationsService: NotificationsService,
44 protected authService: AuthService,
45 protected serverService: ServerService,
46 protected videoService: VideoService,
47 protected videoCaptionService: VideoCaptionService,
48 private router: Router,
49 private videoImportService: VideoImportService,
50 private i18n: I18n
51 ) {
52 super()
53 }
54
55 ngOnInit () {
56 super.ngOnInit()
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: this.firstStepPrivacyId,
72 waitTranscoding: false,
73 commentsEnabled: 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 support: null,
89 thumbnailUrl: null,
90 previewUrl: null
91 }))
92
93 this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies)
94
95 this.hydrateFormFromVideo()
96 },
97
98 err => {
99 this.loadingBar.complete()
100 this.isImportingVideo = false
101 this.firstStepError.emit()
102 this.notificationsService.error(this.i18n('Error'), err.message)
103 }
104 )
105 }
106
107 updateSecondStep () {
108 if (this.checkForm() === false) {
109 return
110 }
111
112 this.video.patch(this.form.value)
113
114 this.isUpdatingVideo = true
115
116 // Update the video
117 this.updateVideoAndCaptions(this.video)
118 .subscribe(
119 () => {
120 this.isUpdatingVideo = false
121 this.notificationsService.success(this.i18n('Success'), this.i18n('Video to import updated.'))
122
123 this.router.navigate([ '/my-account', 'video-imports' ])
124 },
125
126 err => {
127 this.error = err.message
128 scrollToTop()
129 console.error(err)
130 }
131 )
132
133 }
134
135 private hydrateFormFromVideo () {
136 this.form.patchValue(this.video.toFormPatch())
137 }
138 }