Skip to content

Commit bfdf6ca

Browse files
committed
feat: implement API and web routes with database initialization
1 parent b4d6377 commit bfdf6ca

File tree

3 files changed

+317
-167
lines changed

3 files changed

+317
-167
lines changed

src/app.ts

Lines changed: 14 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -1,182 +1,29 @@
11
import express, { Request, Response } from 'express';
2-
import { configured, getAllowOrigin } from './utils/config.js';
3-
import { getImage, uploadImages } from './controllers/images.controller.js';
4-
import { FilesController, uploadFiles } from './controllers/files.controller.js';
5-
import { FolderController } from './controllers/folder.controller.js';
6-
import { sendNotFound, sendSuccess } from './utils/response.js';
7-
import { PreviewController } from './controllers/preview.controller.js';
8-
2+
import { sendNotFound } from './utils/response.js';
3+
import { configured } from './utils/config.js';
4+
import { Database } from './utils/database.js';
5+
import API from './routes/api.js';
6+
import WEB from './routes/web.js';
97
const app = express();
108

119
/**
12-
* Middleware to parse JSON and urlencoded bodies
13-
*/
14-
app.use(express.json({ limit: '500mb' }));
15-
app.use(express.urlencoded({ extended: true, limit: '500mb' }));
16-
17-
/**
18-
* Middleware to parse JSON bodies
19-
*/
20-
app.use((req, res, next) => {
21-
const allowedOrigins = getAllowOrigin();
22-
23-
const origin = req.headers.origin;
24-
if (origin) {
25-
const isAllowed = allowedOrigins.some(allowed =>
26-
typeof allowed === 'string' ? allowed === origin : allowed.test(origin)
27-
);
28-
29-
if (isAllowed) {
30-
res.header('Access-Control-Allow-Origin', origin);
31-
}
32-
}
33-
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
34-
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization')
35-
36-
next()
37-
})
38-
39-
/**
40-
* Default End point
41-
* @method GET /
42-
*/
43-
app.get('/', (request: Request, response: Response) => {
44-
sendSuccess(response, request.query, 'Welcome to Assets Service', 200);
45-
});
46-
47-
/**
48-
* Image optimization endpoint
49-
* @method GET /source/v1/files/image/:filename
50-
*
51-
* Query Parameters:
52-
* - fm: Format (e.g., jpg, png, webp)
53-
* - q: Quality (e.g., 80)
54-
* - w: Width (e.g., 300)
55-
* - h: Height (e.g., 300)
56-
* - fit: Fit mode (e.g., cover, contain)
57-
*/
58-
app.get('/source/v1/files/image/:filename', getImage);
59-
60-
/**
61-
* Image upload endpoint
62-
*
63-
* @method POST /image/upload
64-
* Form Data:
65-
* - images: Images file to upload
10+
* Initialize Database
6611
*/
67-
app.post('/image/upload', uploadImages);
68-
69-
/**
70-
* @title File upload endpoint
71-
*
72-
* @method POST /file/upload
73-
* --------------------------------------------------
74-
* @description Multipart Form Data Upload
75-
* --------------------------------------------------
76-
* Headers: {
77-
* Content-Type: multipart/form-data,
78-
* storage: string (optional),
79-
* X-Prefix: string (optional)
80-
* }
81-
*
82-
* Form Data:
83-
* - files: Files to upload
84-
*
85-
* --------------------------------------------------
86-
* @description Single File Upload via JSON Body
87-
* --------------------------------------------------
88-
* Headers: {
89-
* Content-Type: application/json
90-
* }
91-
*
92-
* JSON Body:
93-
* - base64: Base64 encoded file content
94-
* - filename: Name of the file
95-
* - mimetype: MIME type of the file
96-
*
97-
* --------------------------------------------------
98-
* @description Multiple File Uploads via JSON Body
99-
* --------------------------------------------------
100-
* Headers: {
101-
* Content-Type: application/json
102-
* }
103-
*
104-
* Body Raw JSON:
105-
* - files: Array of files with base64, filename, mimetype
106-
*
107-
*/
108-
app.post('/file/upload', (req, res) => {
109-
110-
/**
111-
* Single File Upload via JSON Body
112-
*/
113-
if (req.is('application/json') && req.body && req.body.base64 && req.body.filename && req.body.mimetype) {
114-
return FilesController.uploadFileBase64(req, res);
115-
}
116-
117-
/**
118-
* Multiple File Uploads via JSON Body
119-
*/
120-
if (req.is('application/json') && req.body && req.body.files && Array.isArray(req.body.files)) {
121-
return FilesController.uploadMultipleFilesBase64(req, res);
122-
}
123-
124-
/**
125-
* Multipart Form Data Upload
126-
*/
127-
return uploadFiles(req, res);
12+
Database.initialize().then(() => {
13+
console.log('✅ Database initialized successfully');
14+
}).catch((error) => {
15+
console.error('❌ Failed to initialize database:', error);
12816
});
12917

130-
131-
/**
132-
* File search endpoint
133-
* @method GET /file/search?q=&type
134-
*
135-
* Query Parameters:
136-
* - q: Name of the file to search
137-
* - type: {image, office} Type of files to search (optional)
138-
*/
139-
app.get('/file/search', FilesController.searchFileByName);
140-
14118
/**
142-
* Move file to directory endpoint
143-
*
144-
* @method PUT /file/move/:filename
145-
*/
146-
app.put('/file/move/:filename', FilesController.moveFileToDir);
147-
148-
149-
/**
150-
* File download endpoint
151-
*
152-
* @method GET /file/download/:filename
19+
* Web UI Routes
15320
*/
154-
app.get('/file/download/:filename', FilesController.downloadFile);
21+
app.use('/', WEB);
15522

15623
/**
157-
* File preview endpoint
158-
*
159-
* @method GET /file/preview/:filename
24+
* API Routes
16025
*/
161-
app.get('/file/preview/:filename', PreviewController.all);
162-
163-
164-
/**
165-
* Get folder structure dynamically based on the route
166-
* @method GET /folder
167-
* Dynamic Path:
168-
* - /folder/ -> Shows top-level folders and files in `storage`
169-
*/
170-
app.get('/folder', FolderController.getFolderStructure);
171-
172-
/**
173-
* Get folder structure dynamically based on the route
174-
* @method GET /folder/*
175-
* Dynamic Path:
176-
* - /folder/ -> Shows top-level folders and files in `storage`
177-
* - /folder/folder1 -> Shows contents of `folder1`
178-
*/
179-
app.get('/folder/*path', FolderController.getFolderStructure);
26+
app.use('/api', API);
18027

18128
/**
18229
* Catch-all route for undefined endpoints

0 commit comments

Comments
 (0)