@@ -2,31 +2,38 @@ import { CloudAPIAction } from "~/api/cloud-action.ts";
22import type { CloudFile } from "../entries/_cloud-file.type.ts" ;
33import MimeTypes from "../mime-types/mime-types.ts" ;
44import { GlobalCloudFile } from "../entries/_global-cloud-file.type.ts" ;
5+ import { joinPath } from "@inspatial/cloud/utils" ;
56
67export const uploadFile = new CloudAPIAction ( "upload" , {
78 label : "Upload File" ,
89 raw : true ,
910 params : [ {
1011 key : "global" ,
1112 type : "BooleanField" ,
13+ } , {
14+ key : "publicFile" ,
15+ type : "BooleanField" ,
1216 } ] ,
13- async run ( { inCloud, orm, inRequest, inResponse, params : { global } } ) {
14- console . log ( { global } ) ;
17+ async run (
18+ { inCloud, orm, inRequest, inResponse, params : { global, publicFile } } ,
19+ ) {
1520 const formData = await inRequest . request . formData ( ) ;
1621 const file = formData . get ( "content" ) as File ;
1722 const fileName = formData . get ( "fileName" ) as string ;
1823 let cloudFile : CloudFile | GlobalCloudFile ;
24+ let accountId = orm . _user ! . accountId ;
1925 switch ( global ) {
2026 case true :
2127 cloudFile = orm . getNewEntry < GlobalCloudFile > ( "globalCloudFile" ) ;
28+ accountId = "global" ;
2229 break ;
2330 default :
2431 cloudFile = orm . getNewEntry < CloudFile > ( "cloudFile" ) ;
2532 }
26- cloudFile = orm . getNewEntry < CloudFile > ( "cloudFile" ) ;
2733 cloudFile . fileName = fileName ;
2834 cloudFile . fileSize = file . size ;
2935 cloudFile . mimeType = file . type as any ;
36+ cloudFile . publicFile = publicFile ;
3037 const extensionInfo = MimeTypes . getExtensionsByMimeType ( file . type ) ;
3138 if ( extensionInfo ) {
3239 cloudFile . fileType = extensionInfo . category ;
@@ -38,8 +45,17 @@ export const uploadFile = new CloudAPIAction("upload", {
3845 const id = cloudFile . id ;
3946 const extension = fileName . split ( "." ) . pop ( ) ;
4047 const newFileName = `${ id } .${ extension } ` ;
48+
4149 const stream = file . stream ( ) ;
42- const path = `${ inCloud . filesPath } /${ newFileName } ` ;
50+ let accountFolder = joinPath ( inCloud . filesPath , accountId ) ;
51+ let path = joinPath ( accountFolder , newFileName ) ;
52+ if ( publicFile ) {
53+ accountFolder = joinPath ( inCloud . publicFilesPath , accountId ) ;
54+ path = joinPath ( accountFolder , newFileName ) ;
55+ }
56+ await Deno . mkdir ( accountFolder , {
57+ recursive : true ,
58+ } ) ;
4359 await Deno . writeFile ( path , stream , {
4460 create : true ,
4561 } ) ;
0 commit comments