diff options
Diffstat (limited to 'client/src/app/+videos/+video-edit/video-add-components/video-import-torrent.component.ts')
-rw-r--r-- | client/src/app/+videos/+video-edit/video-add-components/video-import-torrent.component.ts | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/client/src/app/+videos/+video-edit/video-add-components/video-import-torrent.component.ts b/client/src/app/+videos/+video-edit/video-add-components/video-import-torrent.component.ts new file mode 100644 index 000000000..538a187a8 --- /dev/null +++ b/client/src/app/+videos/+video-edit/video-add-components/video-import-torrent.component.ts | |||
@@ -0,0 +1,147 @@ | |||
1 | import { Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core' | ||
2 | import { Router } from '@angular/router' | ||
3 | import { AuthService, CanComponentDeactivate, Notifier, ServerService } from '@app/core' | ||
4 | import { scrollToTop } from '@app/helpers' | ||
5 | import { FormValidatorService } from '@app/shared/shared-forms' | ||
6 | import { VideoCaptionService, VideoEdit, VideoImportService, VideoService } from '@app/shared/shared-main' | ||
7 | import { VideoSend } from './video-send' | ||
8 | import { LoadingBarService } from '@ngx-loading-bar/core' | ||
9 | import { I18n } from '@ngx-translate/i18n-polyfill' | ||
10 | import { VideoPrivacy, VideoUpdate } from '@shared/models' | ||
11 | |||
12 | @Component({ | ||
13 | selector: 'my-video-import-torrent', | ||
14 | templateUrl: './video-import-torrent.component.html', | ||
15 | styleUrls: [ | ||
16 | '../shared/video-edit.component.scss', | ||
17 | './video-import-torrent.component.scss', | ||
18 | './video-send.scss' | ||
19 | ] | ||
20 | }) | ||
21 | export class VideoImportTorrentComponent extends VideoSend implements OnInit, CanComponentDeactivate { | ||
22 | @Output() firstStepDone = new EventEmitter<string>() | ||
23 | @Output() firstStepError = new EventEmitter<void>() | ||
24 | @ViewChild('torrentfileInput') torrentfileInput: ElementRef<HTMLInputElement> | ||
25 | |||
26 | magnetUri = '' | ||
27 | |||
28 | isImportingVideo = false | ||
29 | hasImportedVideo = false | ||
30 | isUpdatingVideo = false | ||
31 | |||
32 | video: VideoEdit | ||
33 | error: string | ||
34 | |||
35 | protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC | ||
36 | |||
37 | constructor ( | ||
38 | protected formValidatorService: FormValidatorService, | ||
39 | protected loadingBar: LoadingBarService, | ||
40 | protected notifier: Notifier, | ||
41 | protected authService: AuthService, | ||
42 | protected serverService: ServerService, | ||
43 | protected videoService: VideoService, | ||
44 | protected videoCaptionService: VideoCaptionService, | ||
45 | private router: Router, | ||
46 | private videoImportService: VideoImportService, | ||
47 | private i18n: I18n | ||
48 | ) { | ||
49 | super() | ||
50 | } | ||
51 | |||
52 | ngOnInit () { | ||
53 | super.ngOnInit() | ||
54 | } | ||
55 | |||
56 | canDeactivate () { | ||
57 | return { canDeactivate: true } | ||
58 | } | ||
59 | |||
60 | isMagnetUrlValid () { | ||
61 | return !!this.magnetUri | ||
62 | } | ||
63 | |||
64 | fileChange () { | ||
65 | const torrentfile = this.torrentfileInput.nativeElement.files[0] | ||
66 | if (!torrentfile) return | ||
67 | |||
68 | this.importVideo(torrentfile) | ||
69 | } | ||
70 | |||
71 | setTorrentFile (files: FileList) { | ||
72 | this.torrentfileInput.nativeElement.files = files | ||
73 | this.fileChange() | ||
74 | } | ||
75 | |||
76 | importVideo (torrentfile?: Blob) { | ||
77 | this.isImportingVideo = true | ||
78 | |||
79 | const videoUpdate: VideoUpdate = { | ||
80 | privacy: this.firstStepPrivacyId, | ||
81 | waitTranscoding: false, | ||
82 | commentsEnabled: true, | ||
83 | downloadEnabled: true, | ||
84 | channelId: this.firstStepChannelId | ||
85 | } | ||
86 | |||
87 | this.loadingBar.start() | ||
88 | |||
89 | this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate).subscribe( | ||
90 | res => { | ||
91 | this.loadingBar.complete() | ||
92 | this.firstStepDone.emit(res.video.name) | ||
93 | this.isImportingVideo = false | ||
94 | this.hasImportedVideo = true | ||
95 | |||
96 | this.video = new VideoEdit(Object.assign(res.video, { | ||
97 | commentsEnabled: videoUpdate.commentsEnabled, | ||
98 | downloadEnabled: videoUpdate.downloadEnabled, | ||
99 | support: null, | ||
100 | thumbnailUrl: null, | ||
101 | previewUrl: null | ||
102 | })) | ||
103 | |||
104 | this.hydrateFormFromVideo() | ||
105 | }, | ||
106 | |||
107 | err => { | ||
108 | this.loadingBar.complete() | ||
109 | this.isImportingVideo = false | ||
110 | this.firstStepError.emit() | ||
111 | this.notifier.error(err.message) | ||
112 | } | ||
113 | ) | ||
114 | } | ||
115 | |||
116 | updateSecondStep () { | ||
117 | if (this.checkForm() === false) { | ||
118 | return | ||
119 | } | ||
120 | |||
121 | this.video.patch(this.form.value) | ||
122 | |||
123 | this.isUpdatingVideo = true | ||
124 | |||
125 | // Update the video | ||
126 | this.updateVideoAndCaptions(this.video) | ||
127 | .subscribe( | ||
128 | () => { | ||
129 | this.isUpdatingVideo = false | ||
130 | this.notifier.success(this.i18n('Video to import updated.')) | ||
131 | |||
132 | this.router.navigate([ '/my-account', 'video-imports' ]) | ||
133 | }, | ||
134 | |||
135 | err => { | ||
136 | this.error = err.message | ||
137 | scrollToTop() | ||
138 | console.error(err) | ||
139 | } | ||
140 | ) | ||
141 | |||
142 | } | ||
143 | |||
144 | private hydrateFormFromVideo () { | ||
145 | this.form.patchValue(this.video.toFormPatch()) | ||
146 | } | ||
147 | } | ||