-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
123 lines (99 loc) · 2.79 KB
/
index.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
import { Elysia, t } from "elysia"
import staticPlugin from "@elysiajs/static"
import {v4 as uuidv4} from 'uuid'
import sharp from "sharp"
import slugify from "@sindresorhus/slugify"
import fs from 'fs'
import { isKeyObject } from "util/types"
import { nextTick } from "process"
const imageSize = [
{ key: 'sm', value : 640 },
{ key: 'md', value : 768 },
{ key: 'lg', value : 1024 },
{ key: 'xl', value : 1280 }
]
var mediaURL = 'http://localhost:3366/'
const app = new Elysia()
.get("/", () => {
let welcome = {
status: 200,
url: 'https://media.mmohub.io/images'
}
return JSON.stringify(welcome)
})
.onError(({ code, error, set }) => {
if (code === 'NOT_FOUND') {
set.status = 404
return '404 - Not Found'
}
})
.post('/upload', async ({ body: {files} }) => {
var data = [];
for (const file of files) {
let pathStorage = 'images';
let uuidImage = uuidv4();
let nameImage = slugify(file.name);
let dirPath = pathStorage + "/" + uuidImage;
let fullPathStorage = dirPath + "/" + nameImage + ".png";
var subData = [];
try {
await fs.promises.mkdir(dirPath, { recursive: true });
} catch (err) {
console.error(err);
return;
}
try {
await Bun.write(fullPathStorage, file);
} catch (err) {
console.error(err);
return;
}
subData.push({
size: 'orignal',
url: mediaURL + fullPathStorage
});
for (const is of imageSize) {
const isImageName = dirPath + "/" + nameImage + '-' + is.key + '.png';
try {
await sharp(fullPathStorage)
.resize({ width: is.value })
.toFile(isImageName);
} catch (err) {
console.error(err);
return;
}
subData.push({
size: is.key,
url: mediaURL + isImageName
});
}
data.push(subData);
}
return JSON.stringify(data);
},
{
body: t.Object({
files: t.Files({
type: ['image'],
maxItems: 10,
maxSize: 5 * 1024 * 1024
})
})
})
// Handle /images/abcxyz return 404 - Not Found
.get('/images/:id', (set) =>{
set.status = 404
return '404 - Not Found'
})
.listen(3366);
// Config use static asset - prefix '/images' is mean url: localhost:3366/images/*
app.use(staticPlugin({
assets: "images",
prefix: "images",
alwaysStatic: false,
ignorePatterns: ['/images/**/','.DS_Store', '.git', '.env'],
noExtension: false,
}))
console.log(
`🦊 Service is running at ${app.server?.hostname}:${app.server?.port}`
)