]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-add-components/video-import-url.component.ts
Add downloadingEnabled property to video model
[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
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-import-url.component.scss'
22 ]
23 })
24 export class VideoImportUrlComponent extends VideoSend implements OnInit, CanComponentDeactivate {
25 @Output() firstStepDone = new EventEmitter<string>()
26
27 targetUrl = ''
28 videoFileName: string
29
30 isImportingVideo = false
31 hasImportedVideo = false
32 isUpdatingVideo = false
33
34 video: VideoEdit
35
36 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
37
38 constructor (
39 protected formValidatorService: FormValidatorService,
40 protected loadingBar: LoadingBarService,
41 protected notificationsService: NotificationsService,
42 protected authService: AuthService,
43 protected serverService: ServerService,
44 protected videoService: VideoService,
45 protected videoCaptionService: VideoCaptionService,
46 private router: Router,
47 private videoImportService: VideoImportService,
48 private i18n: I18n
49 ) {
50 super()
51 }
52
53 ngOnInit () {
54 super.ngOnInit()
55 }
56
57 canDeactivate () {
58 return { canDeactivate: true }
59 }
60
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 downloadingEnabled: true,
73 channelId: this.firstStepChannelId
74 }
75
76 this.loadingBar.start()
77
78 this.videoImportService.importVideoUrl(this.targetUrl, videoUpdate).subscribe(
79 res => {
80 this.loadingBar.complete()
81 this.firstStepDone.emit(res.video.name)
82 this.isImportingVideo = false
83 this.hasImportedVideo = true
84
85 this.video = new VideoEdit(Object.assign(res.video, {
86 commentsEnabled: videoUpdate.commentsEnabled,
87 downloadingEnabled: videoUpdate.downloadingEnabled,
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.notificationsService.error(this.i18n('Error'), err.message)
102 }
103 )
104 }
105
106 updateSecondStep () {
107 if (this.checkForm() === false) {
108 return
109 }
110
111 this.video.patch(this.form.value)
112
113 this.isUpdatingVideo = true
114
115 // Update the video
116 this.updateVideoAndCaptions(this.video)
117 .subscribe(
118 () => {
119 this.isUpdatingVideo = false
120 this.notificationsService.success(this.i18n('Success'), this.i18n('Video to import updated.'))
121
122 this.router.navigate([ '/my-account', 'video-imports' ])
123 },
124
125 err => {
126 this.isUpdatingVideo = false
127 this.notificationsService.error(this.i18n('Error'), err.message)
128 console.error(err)
129 }
130 )
131
132 }
133
134 private hydrateFormFromVideo () {
135 this.form.patchValue(this.video.toFormPatch())
136 }
137 }