-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo.service.ts
130 lines (107 loc) · 4.01 KB
/
video.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { VideoEntity } from '~/bundles/videos/video.entity.js';
import { type VideoRepository } from '~/bundles/videos/video.repository.js';
import { HTTPCode, HttpError } from '~/common/http/http.js';
import { type ImageService } from '~/common/services/image/image.service.js';
import { type RemotionService } from '~/common/services/remotion/remotion.service.js';
import { tokenService } from '~/common/services/services.js';
import { type Service } from '~/common/types/types.js';
import { VideoValidationMessage } from './enums/enums.js';
import {
type CreateVideoRequestDto,
type Scene,
type UpdateVideoRequestDto,
type VideoGetAllItemResponseDto,
type VideoGetAllResponseDto,
} from './types/types.js';
class VideoService implements Service {
private videoRepository: VideoRepository;
private remotionService: RemotionService;
private imageService: ImageService;
public constructor(
videoRepository: VideoRepository,
remotionService: RemotionService,
imageService: ImageService,
) {
this.videoRepository = videoRepository;
this.remotionService = remotionService;
this.imageService = imageService;
}
public async findById(id: string): Promise<VideoGetAllItemResponseDto> {
const video = await this.videoRepository.findById(id);
if (!video) {
throw new HttpError({
message: VideoValidationMessage.VIDEO_DOESNT_EXIST,
status: HTTPCode.NOT_FOUND,
});
}
return video.toObject();
}
public async findByUserId(userId: string): Promise<VideoGetAllResponseDto> {
const items = await this.videoRepository.findByUserId(userId);
// eslint-disable-next-line no-console
console.log(userId);
// eslint-disable-next-line no-console
console.log(items);
return {
items: items.map((it) => it.toObject()),
};
}
public async findAll(): Promise<VideoGetAllResponseDto> {
const items = await this.videoRepository.findAll();
return {
items: items.map((it) => it.toObject()),
};
}
public async create(
payload: CreateVideoRequestDto & { userId: string },
): Promise<VideoGetAllItemResponseDto> {
const previewUrl = await this.imageService.generatePreview(
payload.composition.scenes[0] as Scene,
);
const video = await this.videoRepository.create(
VideoEntity.initializeNew({
name: payload.name,
composition: payload.composition,
previewUrl,
userId: payload.userId,
}),
);
return video.toObject();
}
public async update(
id: string,
payload: UpdateVideoRequestDto,
): Promise<VideoGetAllItemResponseDto> {
const updatedVideo = await this.videoRepository.update(id, payload);
if (!updatedVideo) {
throw new HttpError({
message: VideoValidationMessage.VIDEO_DOESNT_EXIST,
status: HTTPCode.NOT_FOUND,
});
}
return updatedVideo.toObject();
}
public async delete(id: string): Promise<boolean> {
const { url } = await this.findById(id);
if (url) {
const renderIdMatch = url.match(/renders\/([^/]+)/);
const renderId = renderIdMatch?.[1];
if (renderId) {
await this.remotionService.deleteRenderedVideo(renderId);
}
}
const isVideoDeleted = await this.videoRepository.delete(id);
if (!isVideoDeleted) {
throw new HttpError({
message: VideoValidationMessage.VIDEO_DOESNT_EXIST,
status: HTTPCode.NOT_FOUND,
});
}
return isVideoDeleted;
}
public async getVideoIdToken(id: string): Promise<string> {
const token = await tokenService.createToken(id, false);
return token.replaceAll('.', '~');
}
}
export { VideoService };