@simo777/fastify-storage 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +193 -0
- package/dist/src/app.d.ts +3 -0
- package/dist/src/app.js +93 -0
- package/dist/src/app.js.map +1 -0
- package/dist/src/config/env.d.ts +26 -0
- package/dist/src/config/env.js +28 -0
- package/dist/src/config/env.js.map +1 -0
- package/dist/src/lib/storage/index.d.ts +20 -0
- package/dist/src/lib/storage/index.js +28 -0
- package/dist/src/lib/storage/index.js.map +1 -0
- package/dist/src/lib/storage/local.d.ts +15 -0
- package/dist/src/lib/storage/local.js +55 -0
- package/dist/src/lib/storage/local.js.map +1 -0
- package/dist/src/lib/storage/provider.d.ts +23 -0
- package/dist/src/lib/storage/provider.js +2 -0
- package/dist/src/lib/storage/provider.js.map +1 -0
- package/dist/src/lib/storage/r2.d.ts +28 -0
- package/dist/src/lib/storage/r2.js +85 -0
- package/dist/src/lib/storage/r2.js.map +1 -0
- package/dist/src/modules/health/index.d.ts +2 -0
- package/dist/src/modules/health/index.js +60 -0
- package/dist/src/modules/health/index.js.map +1 -0
- package/dist/src/modules/storage/index.d.ts +2 -0
- package/dist/src/modules/storage/index.js +76 -0
- package/dist/src/modules/storage/index.js.map +1 -0
- package/dist/src/modules/storage/storage.controller.d.ts +119 -0
- package/dist/src/modules/storage/storage.controller.js +99 -0
- package/dist/src/modules/storage/storage.controller.js.map +1 -0
- package/dist/src/modules/storage/storage.service.d.ts +111 -0
- package/dist/src/modules/storage/storage.service.js +191 -0
- package/dist/src/modules/storage/storage.service.js.map +1 -0
- package/dist/src/plugins/prisma.d.ts +9 -0
- package/dist/src/plugins/prisma.js +18 -0
- package/dist/src/plugins/prisma.js.map +1 -0
- package/dist/src/plugins/queues.d.ts +14 -0
- package/dist/src/plugins/queues.js +29 -0
- package/dist/src/plugins/queues.js.map +1 -0
- package/dist/src/plugins/redis.d.ts +9 -0
- package/dist/src/plugins/redis.js +15 -0
- package/dist/src/plugins/redis.js.map +1 -0
- package/dist/src/plugins/storage.d.ts +14 -0
- package/dist/src/plugins/storage.js +15 -0
- package/dist/src/plugins/storage.js.map +1 -0
- package/dist/src/server.d.ts +1 -0
- package/dist/src/server.js +19 -0
- package/dist/src/server.js.map +1 -0
- package/dist/src/workers/index.d.ts +1 -0
- package/dist/src/workers/index.js +33 -0
- package/dist/src/workers/index.js.map +1 -0
- package/package.json +77 -0
- package/prisma/schema.prisma +28 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Type } from '@sinclair/typebox';
|
|
2
|
+
export default async function (fastify) {
|
|
3
|
+
fastify.get('/', {
|
|
4
|
+
schema: {
|
|
5
|
+
tags: ['health'],
|
|
6
|
+
response: {
|
|
7
|
+
200: Type.Object({
|
|
8
|
+
status: Type.String(),
|
|
9
|
+
uptime: Type.Number(),
|
|
10
|
+
timestamp: Type.String()
|
|
11
|
+
})
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}, async () => {
|
|
15
|
+
return {
|
|
16
|
+
status: 'ok',
|
|
17
|
+
uptime: process.uptime(),
|
|
18
|
+
timestamp: new Date().toISOString()
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
fastify.get('/ready', {
|
|
22
|
+
schema: {
|
|
23
|
+
tags: ['health'],
|
|
24
|
+
response: {
|
|
25
|
+
200: Type.Object({
|
|
26
|
+
status: Type.String(),
|
|
27
|
+
checks: Type.Object({
|
|
28
|
+
database: Type.String(),
|
|
29
|
+
redis: Type.String()
|
|
30
|
+
}),
|
|
31
|
+
timestamp: Type.String()
|
|
32
|
+
}),
|
|
33
|
+
503: Type.Object({
|
|
34
|
+
status: Type.String(),
|
|
35
|
+
checks: Type.Object({
|
|
36
|
+
database: Type.String(),
|
|
37
|
+
redis: Type.String()
|
|
38
|
+
}),
|
|
39
|
+
timestamp: Type.String()
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}, async (_request, reply) => {
|
|
44
|
+
const [database, redis] = await Promise.allSettled([
|
|
45
|
+
fastify.prisma.$queryRaw `SELECT 1`,
|
|
46
|
+
fastify.redis.ping()
|
|
47
|
+
]);
|
|
48
|
+
const checks = {
|
|
49
|
+
database: database.status === 'fulfilled' ? 'ok' : 'error',
|
|
50
|
+
redis: redis.status === 'fulfilled' ? 'ok' : 'error'
|
|
51
|
+
};
|
|
52
|
+
const ready = Object.values(checks).every((status) => status === 'ok');
|
|
53
|
+
return reply.code(ready ? 200 : 503).send({
|
|
54
|
+
status: ready ? 'ready' : 'not_ready',
|
|
55
|
+
checks,
|
|
56
|
+
timestamp: new Date().toISOString()
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/modules/health/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAGzC,MAAM,CAAC,OAAO,CAAC,KAAK,WAAW,OAAwB;IACrD,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE;QACf,MAAM,EAAE;YACN,IAAI,EAAE,CAAC,QAAQ,CAAC;YAChB,QAAQ,EAAE;gBACR,GAAG,EAAE,IAAI,CAAC,MAAM,CAAC;oBACf,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE;oBACrB,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE;oBACrB,SAAS,EAAE,IAAI,CAAC,MAAM,EAAE;iBACzB,CAAC;aACH;SACF;KACF,EAAE,KAAK,IAAI,EAAE;QACZ,OAAO;YACL,MAAM,EAAE,IAAI;YACZ,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE;YACxB,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;SACpC,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE;QACp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AM,KAAK,IAAI,CAAC,CAAC;QAEvE,OAAO,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;YACxC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW;YACrC,MAAM;YACN,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;SACpC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { StorageController } from './storage.controller.js';
|
|
2
|
+
import { StorageService } from './storage.service.js';
|
|
3
|
+
import { Type } from '@sinclair/typebox';
|
|
4
|
+
export default async function (fastify) {
|
|
5
|
+
const service = new StorageService(fastify);
|
|
6
|
+
const controller = new StorageController(service);
|
|
7
|
+
fastify.post('/upload', {
|
|
8
|
+
schema: {
|
|
9
|
+
response: {
|
|
10
|
+
200: Type.Object({
|
|
11
|
+
id: Type.String(),
|
|
12
|
+
key: Type.String(),
|
|
13
|
+
url: Type.String(),
|
|
14
|
+
originalName: Type.String(),
|
|
15
|
+
mimeType: Type.String(),
|
|
16
|
+
size: Type.Number(),
|
|
17
|
+
type: Type.String(),
|
|
18
|
+
hash: Type.String(),
|
|
19
|
+
width: Type.Optional(Type.Number()),
|
|
20
|
+
height: Type.Optional(Type.Number()),
|
|
21
|
+
duration: Type.Optional(Type.Number()),
|
|
22
|
+
}),
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
handler: controller.upload.bind(controller),
|
|
26
|
+
});
|
|
27
|
+
fastify.put('/:id', {
|
|
28
|
+
schema: {
|
|
29
|
+
params: Type.Object({
|
|
30
|
+
id: Type.String(),
|
|
31
|
+
}),
|
|
32
|
+
},
|
|
33
|
+
handler: controller.replace.bind(controller),
|
|
34
|
+
});
|
|
35
|
+
fastify.patch('/:id', {
|
|
36
|
+
schema: {
|
|
37
|
+
params: Type.Object({
|
|
38
|
+
id: Type.String(),
|
|
39
|
+
}),
|
|
40
|
+
body: Type.Object({
|
|
41
|
+
originalName: Type.Optional(Type.String()),
|
|
42
|
+
}),
|
|
43
|
+
},
|
|
44
|
+
handler: controller.patch.bind(controller),
|
|
45
|
+
});
|
|
46
|
+
fastify.get('/signed-url', {
|
|
47
|
+
schema: {
|
|
48
|
+
querystring: Type.Object({
|
|
49
|
+
key: Type.String(),
|
|
50
|
+
contentType: Type.Optional(Type.String()),
|
|
51
|
+
}),
|
|
52
|
+
},
|
|
53
|
+
handler: controller.getSignedUrl.bind(controller),
|
|
54
|
+
});
|
|
55
|
+
fastify.get('/', {
|
|
56
|
+
schema: {},
|
|
57
|
+
handler: controller.list.bind(controller),
|
|
58
|
+
});
|
|
59
|
+
fastify.get('/:id', {
|
|
60
|
+
schema: {
|
|
61
|
+
params: Type.Object({
|
|
62
|
+
id: Type.String(),
|
|
63
|
+
}),
|
|
64
|
+
},
|
|
65
|
+
handler: controller.get.bind(controller),
|
|
66
|
+
});
|
|
67
|
+
fastify.delete('/:id', {
|
|
68
|
+
schema: {
|
|
69
|
+
params: Type.Object({
|
|
70
|
+
id: Type.String(),
|
|
71
|
+
}),
|
|
72
|
+
},
|
|
73
|
+
handler: controller.delete.bind(controller),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/modules/storage/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAEzC,MAAM,CAAC,OAAO,CAAC,KAAK,WAAW,OAAwB;IACrD,MAAM,OAAO,GAAG,IAAI,cAAc,CAAC,OAAO,CAAC,CAAC;IAC5C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1,119 @@
|
|
|
1
|
+
import { FastifyReply, FastifyRequest } from 'fastify';
|
|
2
|
+
import { StorageService } from './storage.service.js';
|
|
3
|
+
export declare class StorageController {
|
|
4
|
+
private service;
|
|
5
|
+
constructor(service: StorageService);
|
|
6
|
+
upload(request: FastifyRequest, reply: FastifyReply): Promise<{
|
|
7
|
+
id: string;
|
|
8
|
+
key: string;
|
|
9
|
+
bucket: string;
|
|
10
|
+
provider: string;
|
|
11
|
+
originalName: string;
|
|
12
|
+
mimeType: string;
|
|
13
|
+
size: number;
|
|
14
|
+
url: string;
|
|
15
|
+
type: string;
|
|
16
|
+
hash: string;
|
|
17
|
+
width: number | null;
|
|
18
|
+
height: number | null;
|
|
19
|
+
duration: number | null;
|
|
20
|
+
createdAt: Date;
|
|
21
|
+
updatedAt: Date;
|
|
22
|
+
}>;
|
|
23
|
+
replace(request: FastifyRequest<{
|
|
24
|
+
Params: {
|
|
25
|
+
id: string;
|
|
26
|
+
};
|
|
27
|
+
}>, reply: FastifyReply): Promise<{
|
|
28
|
+
id: string;
|
|
29
|
+
key: string;
|
|
30
|
+
bucket: string;
|
|
31
|
+
provider: string;
|
|
32
|
+
originalName: string;
|
|
33
|
+
mimeType: string;
|
|
34
|
+
size: number;
|
|
35
|
+
url: string;
|
|
36
|
+
type: string;
|
|
37
|
+
hash: string;
|
|
38
|
+
width: number | null;
|
|
39
|
+
height: number | null;
|
|
40
|
+
duration: number | null;
|
|
41
|
+
createdAt: Date;
|
|
42
|
+
updatedAt: Date;
|
|
43
|
+
}>;
|
|
44
|
+
patch(request: FastifyRequest<{
|
|
45
|
+
Params: {
|
|
46
|
+
id: string;
|
|
47
|
+
};
|
|
48
|
+
Body: {
|
|
49
|
+
originalName?: string;
|
|
50
|
+
};
|
|
51
|
+
}>, reply: FastifyReply): Promise<{
|
|
52
|
+
id: string;
|
|
53
|
+
key: string;
|
|
54
|
+
bucket: string;
|
|
55
|
+
provider: string;
|
|
56
|
+
originalName: string;
|
|
57
|
+
mimeType: string;
|
|
58
|
+
size: number;
|
|
59
|
+
url: string;
|
|
60
|
+
type: string;
|
|
61
|
+
hash: string;
|
|
62
|
+
width: number | null;
|
|
63
|
+
height: number | null;
|
|
64
|
+
duration: number | null;
|
|
65
|
+
createdAt: Date;
|
|
66
|
+
updatedAt: Date;
|
|
67
|
+
}>;
|
|
68
|
+
getSignedUrl(request: FastifyRequest<{
|
|
69
|
+
Querystring: {
|
|
70
|
+
key: string;
|
|
71
|
+
contentType?: string;
|
|
72
|
+
};
|
|
73
|
+
}>, reply: FastifyReply): Promise<{
|
|
74
|
+
url: string;
|
|
75
|
+
}>;
|
|
76
|
+
list(request: FastifyRequest, reply: FastifyReply): Promise<{
|
|
77
|
+
id: string;
|
|
78
|
+
key: string;
|
|
79
|
+
bucket: string;
|
|
80
|
+
provider: string;
|
|
81
|
+
originalName: string;
|
|
82
|
+
mimeType: string;
|
|
83
|
+
size: number;
|
|
84
|
+
url: string;
|
|
85
|
+
type: string;
|
|
86
|
+
hash: string;
|
|
87
|
+
width: number | null;
|
|
88
|
+
height: number | null;
|
|
89
|
+
duration: number | null;
|
|
90
|
+
createdAt: Date;
|
|
91
|
+
updatedAt: Date;
|
|
92
|
+
}[]>;
|
|
93
|
+
get(request: FastifyRequest<{
|
|
94
|
+
Params: {
|
|
95
|
+
id: string;
|
|
96
|
+
};
|
|
97
|
+
}>, reply: FastifyReply): Promise<{
|
|
98
|
+
id: string;
|
|
99
|
+
key: string;
|
|
100
|
+
bucket: string;
|
|
101
|
+
provider: string;
|
|
102
|
+
originalName: string;
|
|
103
|
+
mimeType: string;
|
|
104
|
+
size: number;
|
|
105
|
+
url: string;
|
|
106
|
+
type: string;
|
|
107
|
+
hash: string;
|
|
108
|
+
width: number | null;
|
|
109
|
+
height: number | null;
|
|
110
|
+
duration: number | null;
|
|
111
|
+
createdAt: Date;
|
|
112
|
+
updatedAt: Date;
|
|
113
|
+
}>;
|
|
114
|
+
delete(request: FastifyRequest<{
|
|
115
|
+
Params: {
|
|
116
|
+
id: string;
|
|
117
|
+
};
|
|
118
|
+
}>, reply: FastifyReply): Promise<never>;
|
|
119
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
export class StorageController {
|
|
2
|
+
service;
|
|
3
|
+
constructor(service) {
|
|
4
|
+
this.service = service;
|
|
5
|
+
}
|
|
6
|
+
async upload(request, reply) {
|
|
7
|
+
let path;
|
|
8
|
+
let fileBuffer;
|
|
9
|
+
let filename;
|
|
10
|
+
let mimetype;
|
|
11
|
+
const parts = request.parts();
|
|
12
|
+
for await (const part of parts) {
|
|
13
|
+
if (part.type === 'file') {
|
|
14
|
+
fileBuffer = await part.toBuffer();
|
|
15
|
+
filename = part.filename;
|
|
16
|
+
mimetype = part.mimetype;
|
|
17
|
+
}
|
|
18
|
+
else if (part.type === 'field' && part.fieldname === 'path') {
|
|
19
|
+
path = part.value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
console.log(path, 'sssssssssssssssjjjj');
|
|
23
|
+
if (!fileBuffer || !filename || !mimetype) {
|
|
24
|
+
return reply.status(400).send({ message: 'No file uploaded' });
|
|
25
|
+
}
|
|
26
|
+
const file = await this.service.uploadFile(fileBuffer, filename, mimetype, path);
|
|
27
|
+
return file;
|
|
28
|
+
}
|
|
29
|
+
async replace(request, reply) {
|
|
30
|
+
let fileBuffer;
|
|
31
|
+
let filename;
|
|
32
|
+
let mimetype;
|
|
33
|
+
const parts = request.parts();
|
|
34
|
+
for await (const part of parts) {
|
|
35
|
+
if (part.type === 'file') {
|
|
36
|
+
fileBuffer = await part.toBuffer();
|
|
37
|
+
filename = part.filename;
|
|
38
|
+
mimetype = part.mimetype;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (!fileBuffer || !filename || !mimetype) {
|
|
42
|
+
return reply.status(400).send({ message: 'No file uploaded' });
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
const file = await this.service.replaceFile(request.params.id, fileBuffer, filename, mimetype);
|
|
46
|
+
return file;
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
if (error.message === 'File not found') {
|
|
50
|
+
return reply.status(404).send({ message: error.message });
|
|
51
|
+
}
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async patch(request, reply) {
|
|
56
|
+
try {
|
|
57
|
+
const file = await this.service.patchMetadata(request.params.id, request.body);
|
|
58
|
+
return file;
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
if (error.message === 'File not found') {
|
|
62
|
+
return reply.status(404).send({ message: error.message });
|
|
63
|
+
}
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async getSignedUrl(request, reply) {
|
|
68
|
+
try {
|
|
69
|
+
const url = await this.service.getSignedUploadUrl(request.query.key, request.query.contentType);
|
|
70
|
+
return { url };
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
return reply.status(400).send({ message: error.message });
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async list(request, reply) {
|
|
77
|
+
return this.service.listFiles();
|
|
78
|
+
}
|
|
79
|
+
async get(request, reply) {
|
|
80
|
+
const file = await this.service.getFile(request.params.id);
|
|
81
|
+
if (!file) {
|
|
82
|
+
return reply.status(404).send({ message: 'File not found' });
|
|
83
|
+
}
|
|
84
|
+
return file;
|
|
85
|
+
}
|
|
86
|
+
async delete(request, reply) {
|
|
87
|
+
try {
|
|
88
|
+
await this.service.deleteFile(request.params.id);
|
|
89
|
+
return reply.status(204).send();
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
if (error.message === 'File not found') {
|
|
93
|
+
return reply.status(404).send({ message: error.message });
|
|
94
|
+
}
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=storage.controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage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D,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,OAAmD,EAAE,KAAmB;QACnF,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;YACjD,OAAO,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;QAClC,CAAC;QAAC,OAAO,KAAU,EAAE,CAAC;YACpB,IAAI,KAAK,CAAC,OAAO,KAAK,gBAAgB,EAAE,CAAC;gBACvC,OAAO,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;YAC5D,CAAC;YACD,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { FastifyInstance } from 'fastify';
|
|
2
|
+
export declare class StorageService {
|
|
3
|
+
private fastify;
|
|
4
|
+
constructor(fastify: FastifyInstance);
|
|
5
|
+
private getMetadata;
|
|
6
|
+
uploadFile(file: Buffer, originalName: string, mimeType: string, path?: string): Promise<{
|
|
7
|
+
id: string;
|
|
8
|
+
key: string;
|
|
9
|
+
bucket: string;
|
|
10
|
+
provider: string;
|
|
11
|
+
originalName: string;
|
|
12
|
+
mimeType: string;
|
|
13
|
+
size: number;
|
|
14
|
+
url: string;
|
|
15
|
+
type: string;
|
|
16
|
+
hash: string;
|
|
17
|
+
width: number | null;
|
|
18
|
+
height: number | null;
|
|
19
|
+
duration: number | null;
|
|
20
|
+
createdAt: Date;
|
|
21
|
+
updatedAt: Date;
|
|
22
|
+
}>;
|
|
23
|
+
replaceFile(id: string, file: Buffer, originalName: string, mimeType: string): Promise<{
|
|
24
|
+
id: string;
|
|
25
|
+
key: string;
|
|
26
|
+
bucket: string;
|
|
27
|
+
provider: string;
|
|
28
|
+
originalName: string;
|
|
29
|
+
mimeType: string;
|
|
30
|
+
size: number;
|
|
31
|
+
url: string;
|
|
32
|
+
type: string;
|
|
33
|
+
hash: string;
|
|
34
|
+
width: number | null;
|
|
35
|
+
height: number | null;
|
|
36
|
+
duration: number | null;
|
|
37
|
+
createdAt: Date;
|
|
38
|
+
updatedAt: Date;
|
|
39
|
+
}>;
|
|
40
|
+
patchMetadata(id: string, data: {
|
|
41
|
+
originalName?: string;
|
|
42
|
+
}): Promise<{
|
|
43
|
+
id: string;
|
|
44
|
+
key: string;
|
|
45
|
+
bucket: string;
|
|
46
|
+
provider: string;
|
|
47
|
+
originalName: string;
|
|
48
|
+
mimeType: string;
|
|
49
|
+
size: number;
|
|
50
|
+
url: string;
|
|
51
|
+
type: string;
|
|
52
|
+
hash: string;
|
|
53
|
+
width: number | null;
|
|
54
|
+
height: number | null;
|
|
55
|
+
duration: number | null;
|
|
56
|
+
createdAt: Date;
|
|
57
|
+
updatedAt: Date;
|
|
58
|
+
}>;
|
|
59
|
+
getSignedUploadUrl(key: string, contentType?: string | undefined): Promise<string>;
|
|
60
|
+
getFile(id: string): Promise<{
|
|
61
|
+
id: string;
|
|
62
|
+
key: string;
|
|
63
|
+
bucket: string;
|
|
64
|
+
provider: string;
|
|
65
|
+
originalName: string;
|
|
66
|
+
mimeType: string;
|
|
67
|
+
size: number;
|
|
68
|
+
url: string;
|
|
69
|
+
type: string;
|
|
70
|
+
hash: string;
|
|
71
|
+
width: number | null;
|
|
72
|
+
height: number | null;
|
|
73
|
+
duration: number | null;
|
|
74
|
+
createdAt: Date;
|
|
75
|
+
updatedAt: Date;
|
|
76
|
+
} | null>;
|
|
77
|
+
deleteFile(id: string): Promise<{
|
|
78
|
+
id: string;
|
|
79
|
+
key: string;
|
|
80
|
+
bucket: string;
|
|
81
|
+
provider: string;
|
|
82
|
+
originalName: string;
|
|
83
|
+
mimeType: string;
|
|
84
|
+
size: number;
|
|
85
|
+
url: string;
|
|
86
|
+
type: string;
|
|
87
|
+
hash: string;
|
|
88
|
+
width: number | null;
|
|
89
|
+
height: number | null;
|
|
90
|
+
duration: number | null;
|
|
91
|
+
createdAt: Date;
|
|
92
|
+
updatedAt: Date;
|
|
93
|
+
}>;
|
|
94
|
+
listFiles(): Promise<{
|
|
95
|
+
id: string;
|
|
96
|
+
key: string;
|
|
97
|
+
bucket: string;
|
|
98
|
+
provider: string;
|
|
99
|
+
originalName: string;
|
|
100
|
+
mimeType: string;
|
|
101
|
+
size: number;
|
|
102
|
+
url: string;
|
|
103
|
+
type: string;
|
|
104
|
+
hash: string;
|
|
105
|
+
width: number | null;
|
|
106
|
+
height: number | null;
|
|
107
|
+
duration: number | null;
|
|
108
|
+
createdAt: Date;
|
|
109
|
+
updatedAt: Date;
|
|
110
|
+
}[]>;
|
|
111
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { randomUUID, createHash } from 'node:crypto';
|
|
2
|
+
import { extname } from 'node:path';
|
|
3
|
+
import sharp from 'sharp';
|
|
4
|
+
import ffmpeg from 'fluent-ffmpeg';
|
|
5
|
+
import { Readable } from 'node:stream';
|
|
6
|
+
export class StorageService {
|
|
7
|
+
fastify;
|
|
8
|
+
constructor(fastify) {
|
|
9
|
+
this.fastify = fastify;
|
|
10
|
+
}
|
|
11
|
+
async getMetadata(buffer, mimeType) {
|
|
12
|
+
let width;
|
|
13
|
+
let height;
|
|
14
|
+
let duration;
|
|
15
|
+
let type = 'file';
|
|
16
|
+
if (mimeType.startsWith('image/')) {
|
|
17
|
+
type = 'image';
|
|
18
|
+
try {
|
|
19
|
+
const metadata = await sharp(buffer).metadata();
|
|
20
|
+
width = metadata.width ?? undefined;
|
|
21
|
+
height = metadata.height ?? undefined;
|
|
22
|
+
}
|
|
23
|
+
catch (e) {
|
|
24
|
+
this.fastify.log.error('Failed to get image metadata', e);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
else if (mimeType.startsWith('video/') || mimeType.startsWith('audio/')) {
|
|
28
|
+
type = mimeType.startsWith('video/') ? 'video' : 'audio';
|
|
29
|
+
try {
|
|
30
|
+
const readable = new Readable();
|
|
31
|
+
readable.push(buffer);
|
|
32
|
+
readable.push(null);
|
|
33
|
+
const metadata = await new Promise((resolve, reject) => {
|
|
34
|
+
ffmpeg(readable).ffprobe((err, data) => {
|
|
35
|
+
if (err)
|
|
36
|
+
reject(err);
|
|
37
|
+
else
|
|
38
|
+
resolve(data);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
duration = metadata.format.duration ?? undefined;
|
|
42
|
+
if (type === 'video') {
|
|
43
|
+
const videoStream = metadata.streams.find(s => s.codec_type === 'video');
|
|
44
|
+
width = videoStream?.width ?? undefined;
|
|
45
|
+
height = videoStream?.height ?? undefined;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
this.fastify.log.error('Failed to get media metadata', e);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return { type, width, height, duration };
|
|
53
|
+
}
|
|
54
|
+
async uploadFile(file, originalName, mimeType, path) {
|
|
55
|
+
const hash = createHash('sha256').update(file).digest('hex');
|
|
56
|
+
// Check for duplicate
|
|
57
|
+
const existing = await this.fastify.prisma.storedFile.findUnique({
|
|
58
|
+
where: { hash },
|
|
59
|
+
});
|
|
60
|
+
if (existing) {
|
|
61
|
+
return existing;
|
|
62
|
+
}
|
|
63
|
+
const { type, width, height, duration } = await this.getMetadata(file, mimeType);
|
|
64
|
+
const extension = extname(originalName);
|
|
65
|
+
const key = `${randomUUID()}${extension}`;
|
|
66
|
+
const provider = this.fastify.storage.provider;
|
|
67
|
+
const finalPath = path || this.fastify.storage.defaultPath;
|
|
68
|
+
const fullKey = await this.fastify.storage.upload(file, {
|
|
69
|
+
key,
|
|
70
|
+
contentType: mimeType,
|
|
71
|
+
path: finalPath,
|
|
72
|
+
});
|
|
73
|
+
const bucket = this.fastify.storage.bucket;
|
|
74
|
+
const url = this.fastify.storage.getPublicUrl(fullKey);
|
|
75
|
+
const storedFile = await this.fastify.prisma.storedFile.create({
|
|
76
|
+
data: {
|
|
77
|
+
key: fullKey,
|
|
78
|
+
bucket,
|
|
79
|
+
provider,
|
|
80
|
+
originalName,
|
|
81
|
+
mimeType,
|
|
82
|
+
size: file.length,
|
|
83
|
+
url,
|
|
84
|
+
hash,
|
|
85
|
+
type,
|
|
86
|
+
width: width ?? null,
|
|
87
|
+
height: height ?? null,
|
|
88
|
+
duration: duration ? Math.round(duration) : null,
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
return storedFile;
|
|
92
|
+
}
|
|
93
|
+
async replaceFile(id, file, originalName, mimeType) {
|
|
94
|
+
const oldFile = await this.getFile(id);
|
|
95
|
+
if (!oldFile) {
|
|
96
|
+
throw new Error('File not found');
|
|
97
|
+
}
|
|
98
|
+
const hash = createHash('sha256').update(file).digest('hex');
|
|
99
|
+
// If new hash matches old hash, just update metadata
|
|
100
|
+
if (hash === oldFile.hash) {
|
|
101
|
+
const { type, width, height, duration } = await this.getMetadata(file, mimeType);
|
|
102
|
+
return this.fastify.prisma.storedFile.update({
|
|
103
|
+
where: { id },
|
|
104
|
+
data: {
|
|
105
|
+
originalName,
|
|
106
|
+
mimeType,
|
|
107
|
+
type,
|
|
108
|
+
width: width ?? null,
|
|
109
|
+
height: height ?? null,
|
|
110
|
+
duration: duration ? Math.round(duration) : null,
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
// Check if new hash exists elsewhere (deduplication)
|
|
115
|
+
const existing = await this.fastify.prisma.storedFile.findUnique({
|
|
116
|
+
where: { hash },
|
|
117
|
+
});
|
|
118
|
+
if (existing) {
|
|
119
|
+
// If a duplicate exists, we delete the current record and return the existing one
|
|
120
|
+
// to maintain global deduplication and clean up the old ID.
|
|
121
|
+
await this.deleteFile(id);
|
|
122
|
+
return existing;
|
|
123
|
+
}
|
|
124
|
+
// Perform full update of the existing record
|
|
125
|
+
const { type, width, height, duration } = await this.getMetadata(file, mimeType);
|
|
126
|
+
const extension = extname(originalName);
|
|
127
|
+
const key = `${randomUUID()}${extension}`;
|
|
128
|
+
const url = this.fastify.storage.getPublicUrl(key);
|
|
129
|
+
const oldKey = oldFile.key;
|
|
130
|
+
await this.fastify.storage.upload(file, {
|
|
131
|
+
key,
|
|
132
|
+
contentType: mimeType,
|
|
133
|
+
});
|
|
134
|
+
const updated = await this.fastify.prisma.storedFile.update({
|
|
135
|
+
where: { id },
|
|
136
|
+
data: {
|
|
137
|
+
key,
|
|
138
|
+
hash,
|
|
139
|
+
originalName,
|
|
140
|
+
mimeType,
|
|
141
|
+
size: file.length,
|
|
142
|
+
url,
|
|
143
|
+
type,
|
|
144
|
+
width: width ?? null,
|
|
145
|
+
height: height ?? null,
|
|
146
|
+
duration: duration ? Math.round(duration) : null,
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
// Clean up old physical file
|
|
150
|
+
await this.fastify.storage.delete(oldKey);
|
|
151
|
+
return updated;
|
|
152
|
+
}
|
|
153
|
+
async patchMetadata(id, data) {
|
|
154
|
+
const file = await this.getFile(id);
|
|
155
|
+
if (!file) {
|
|
156
|
+
throw new Error('File not found');
|
|
157
|
+
}
|
|
158
|
+
return this.fastify.prisma.storedFile.update({
|
|
159
|
+
where: { id },
|
|
160
|
+
data,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
async getSignedUploadUrl(key, contentType) {
|
|
164
|
+
if (!this.fastify.storage.generateSignedUploadUrl) {
|
|
165
|
+
throw new Error('Signed URLs are not supported by the current storage provider');
|
|
166
|
+
}
|
|
167
|
+
return this.fastify.storage.generateSignedUploadUrl(key, { contentType });
|
|
168
|
+
}
|
|
169
|
+
async getFile(id) {
|
|
170
|
+
return this.fastify.prisma.storedFile.findUnique({
|
|
171
|
+
where: { id },
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
async deleteFile(id) {
|
|
175
|
+
const file = await this.getFile(id);
|
|
176
|
+
if (!file) {
|
|
177
|
+
throw new Error('File not found');
|
|
178
|
+
}
|
|
179
|
+
await this.fastify.storage.delete(file.key);
|
|
180
|
+
await this.fastify.prisma.storedFile.delete({
|
|
181
|
+
where: { id },
|
|
182
|
+
});
|
|
183
|
+
return file;
|
|
184
|
+
}
|
|
185
|
+
async listFiles() {
|
|
186
|
+
return this.fastify.prisma.storedFile.findMany({
|
|
187
|
+
orderBy: { createdAt: 'desc' },
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
//# sourceMappingURL=storage.service.js.map
|