127 lines
2.7 KiB
Markdown
127 lines
2.7 KiB
Markdown
# Upload API
|
|
|
|
API endpoint để upload các loại file khác nhau.
|
|
|
|
## Quy tắc phân loại file
|
|
|
|
- **Images** (jpg, jpeg, png, gif, bmp, webp, svg, ico) → `public/images/`
|
|
- **Audio** (mp3, wav, ogg, m4a, aac, flac, wma) → `public/media/`
|
|
- **Video** (mp4, avi, mov, wmv, flv, mkv, webm) → `public/media/`
|
|
- **Các file khác** → `public/files/`
|
|
|
|
## API Endpoints
|
|
|
|
### 1. Upload single file
|
|
```
|
|
POST /api/upload/single
|
|
Content-Type: multipart/form-data
|
|
|
|
Body:
|
|
- file: (binary file)
|
|
```
|
|
|
|
**Response:**
|
|
```json
|
|
{
|
|
"success": true,
|
|
"message": "File uploaded successfully",
|
|
"data": {
|
|
"filename": "example-1234567890.jpg",
|
|
"originalname": "example.jpg",
|
|
"mimetype": "image/jpeg",
|
|
"size": 12345,
|
|
"type": "image",
|
|
"path": "public/images/example-1234567890.jpg",
|
|
"url": "/public/images/example-1234567890.jpg"
|
|
}
|
|
}
|
|
```
|
|
|
|
### 2. Upload multiple files
|
|
```
|
|
POST /api/upload/multiple
|
|
Content-Type: multipart/form-data
|
|
|
|
Body:
|
|
- files: (array of binary files, max 10)
|
|
```
|
|
|
|
**Response:**
|
|
```json
|
|
{
|
|
"success": true,
|
|
"message": "2 files uploaded successfully",
|
|
"data": [
|
|
{
|
|
"filename": "photo1-1234567890.jpg",
|
|
"originalname": "photo1.jpg",
|
|
"mimetype": "image/jpeg",
|
|
"size": 12345,
|
|
"type": "image",
|
|
"path": "public/images/photo1-1234567890.jpg",
|
|
"url": "/public/images/photo1-1234567890.jpg"
|
|
},
|
|
{
|
|
"filename": "audio-1234567891.mp3",
|
|
"originalname": "audio.mp3",
|
|
"mimetype": "audio/mpeg",
|
|
"size": 54321,
|
|
"type": "audio",
|
|
"path": "public/media/audio-1234567891.mp3",
|
|
"url": "/public/media/audio-1234567891.mp3"
|
|
}
|
|
]
|
|
}
|
|
```
|
|
|
|
### 3. Delete file
|
|
```
|
|
DELETE /api/upload/file
|
|
Content-Type: application/json
|
|
|
|
Body:
|
|
{
|
|
"filepath": "public/images/example-1234567890.jpg"
|
|
}
|
|
```
|
|
|
|
### 4. Get file info
|
|
```
|
|
GET /api/upload/info?filepath=public/images/example-1234567890.jpg
|
|
```
|
|
|
|
## Giới hạn
|
|
|
|
- **Max file size:** 50MB
|
|
- **Max files per upload (multiple):** 10 files
|
|
|
|
## Sử dụng với Postman/cURL
|
|
|
|
### Postman:
|
|
1. Chọn POST method
|
|
2. URL: `http://localhost:10001/api/upload/single`
|
|
3. Body → form-data
|
|
4. Key: `file`, Type: File
|
|
5. Chọn file cần upload
|
|
|
|
### cURL:
|
|
```bash
|
|
# Upload single file
|
|
curl -X POST http://localhost:10001/api/upload/single \
|
|
-F "file=@/path/to/your/file.jpg"
|
|
|
|
# Upload multiple files
|
|
curl -X POST http://localhost:10001/api/upload/multiple \
|
|
-F "files=@/path/to/file1.jpg" \
|
|
-F "files=@/path/to/file2.mp3"
|
|
```
|
|
|
|
## Truy cập file đã upload
|
|
|
|
Sau khi upload thành công, file có thể truy cập qua URL:
|
|
```
|
|
http://localhost:10001/public/images/example-1234567890.jpg
|
|
http://localhost:10001/public/media/audio-1234567890.mp3
|
|
http://localhost:10001/public/files/document-1234567890.pdf
|
|
```
|