Skip to content

Commit

Permalink
Optimize upload component
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Oct 29, 2024
1 parent a18dd0d commit 85ad308
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
logs/application.log
logs
.DS_Store
data
db
Expand Down
64 changes: 62 additions & 2 deletions forms/hero.form.yao
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,12 @@
{ "name": "Divider", "width": 24 },
{ "name": "Upload chunckSize", "width": 24 },
{ "name": "Upload chunckSize Multiple", "width": 24 },
{ "name": "Upload chunckSize API", "width": 24 }
{ "name": "Upload chunckSize API", "width": 24 },
{ "name": "Divider", "width": 24 },
{ "name": "Upload Progress", "width": 24 },
{ "name": "Upgraded Progress (Chunk)", "width": 24 },
{ "name": "Upload Error", "width": 24 },
{ "name": "Upload Error (Chunk)", "width": 24 }
]
}
]
Expand Down Expand Up @@ -916,7 +921,62 @@
"maxSize": "1G",
"chunkSize": "2M",
"previewURL": "/assets/upload/chunks[[ $PATH ]]",
"$api": { "process": "scripts.upload.Chunk" }
"$api": { "process": "scripts.upload.ChunkToPublic" }
}
}
},

"Upload Progress": {
"bind": "upload_progress",
"edit": {
"type": "Upload",
"props": {
"filetype": "video",
"accept": ".mp4,.avi,.mov,.wmv",
"maxSize": "1G"
}
}
},

"Upgraded Progress (Chunk)": {
"bind": "upload_progress_chunk",
"edit": {
"type": "Upload",
"props": {
"filetype": "video",
"accept": ".mp4,.avi,.mov,.wmv",
"maxSize": "1G",
"chunkSize": "2M",
"previewURL": "/assets/upload/chunks[[ $PATH ]]",
"$api": { "process": "scripts.upload.ChunkToPublic" }
}
}
},

"Upload Error": {
"bind": "upload_error",
"edit": {
"type": "Upload",
"props": {
"filetype": "video",
"accept": ".mp4,.avi,.mov,.wmv",
"maxSize": "1G",
"$api": { "process": "scripts.upload.ErrorResponse" }
}
}
},

"Upload Error (Chunk)": {
"bind": "upload_error_chunk",
"edit": {
"type": "Upload",
"props": {
"filetype": "video",
"accept": ".mp4,.avi,.mov,.wmv",
"maxSize": "1G",
"chunkSize": "1M",
"placeholder": "Select Video more than 10M",
"$api": { "process": "scripts.upload.ErrorChunkResponse" }
}
}
}
Expand Down
57 changes: 55 additions & 2 deletions scripts/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Exception,
FS,
Process,
Store,
UploadFile,
UploadFileResponse,
} from "@yao/runtime";
Expand Down Expand Up @@ -52,7 +53,7 @@ function ImageToPublicWithAdditional(file: UploadFile): UploadFileResponse {
* @param file
* @returns
*/
function Chunk(file: UploadFile): UploadFileResponse {
function ChunkToPublic(file: UploadFile): UploadFileResponse {
if (!file.range) {
throw new Exception("Get error response. range is empty", 500);
}
Expand Down Expand Up @@ -91,7 +92,59 @@ function Chunk(file: UploadFile): UploadFileResponse {
* @param file
*/
function ErrorResponse(file: UploadFile) {
throw new Exception(`Get error response. name: ${file.name}`, 500);
const now = new Date().toISOString();
throw new Exception(`Error response ${now}`, 500);
}

/**
* Chuk upload error response
* @param file
*/
function ErrorChunkResponse(file: UploadFile) {
const cache = new Store("cache");
const key = `error-chunk-${file.uid}`;

// Get the error chunk
const chunk = parseInt(cache.Get(key)) || 0;
if (chunk > 9) {
cache.Del(key);
throw new Exception(`Error Chunk Response ${chunk}`, 500);
}

cache.Set(key, chunk + 1);
return `/public/assets/upload/chunks/${chunk}`;
}

/**
* Backend API security check
* Test API for upload file
* yao run scripts.upload.Security [host]
* @param host
* @param file
*/
function Security(host?: string) {
host = host || "http://localhost:5099";
host = host.endsWith("/") ? host.slice(0, -1) : host;

const tests = [
{ file: "/tests/security/success.jpg" },
{ file: "/tests/security/error-large.jpg", error: true }, // File size is too large
{ file: "/tests/security/error-type.jpg", error: true }, // Exetension crorect, but content-type is not image
{ file: "/tests/security/error-ext.exe", error: true }, // Exetension is not image
];

const urls = [
`${host}/api/__yao/form/hero/upload/fields.form.Upload+Image.edit.props/api`, // Image upload
`${host}/api/__yao/form/hero/upload/fields.form.Upload+chunckSize+Multiple.edit.props/api`, // Chunk upload
];

// Check Authorization

// Check file size

// Check file extension

// Check file content-type
}

/**
Expand Down
6 changes: 6 additions & 0 deletions stores/cache.lru.yao
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "LRU Cache",
"description": "LRU cache",
"type": "lru",
"option": { "size": 1024 }
}

0 comments on commit 85ad308

Please sign in to comment.