@@ -13,6 +13,8 @@ import { isFirefox } from "@App/pkg/utils/utils";
1313import Hook from "@App/app/service/hook" ;
1414import IoC from "@App/app/ioc" ;
1515import { SystemConfig } from "@App/pkg/config/config" ;
16+ import FileSystemFactory from "@Pkg/filesystem/factory" ;
17+ import FileSystem from "@Pkg/filesystem/filesystem" ;
1618import PermissionVerify , {
1719 ConfirmParam ,
1820 IPermissionVerify ,
@@ -755,4 +757,108 @@ export default class GMApi {
755757 active : true ,
756758 } ) ;
757759 }
760+
761+ @PermissionVerify . API ( {
762+ confirm : ( request : Request ) => {
763+ return Promise . resolve ( {
764+ permission : "file_storage" ,
765+ permissionValue : "*" ,
766+ title : "脚本正在试图操作脚本同步储存空间" ,
767+ metadata : {
768+ 脚本名称 : request . script . name ,
769+ } ,
770+ describe : `请您确认是否允许脚本进行此操作,允许后将允许脚本操作你的脚本同步储存空间,会在储存空间下创建一个app/${ request . script . uuid } 的目录供给脚本使用` ,
771+ wildcard : true ,
772+ permissionContent : "脚本" ,
773+ } as ConfirmParam ) ;
774+ } ,
775+ alias : [ "GM.xmlHttpRequest" ] ,
776+ } )
777+ // eslint-disable-next-line consistent-return
778+ async CAT_fileStorage ( request : Request , channel : Channel ) {
779+ const config = this . systemConfig . cloudSync ;
780+ if ( ! config . enable ) {
781+ return channel . throw ( { code : 1 , error : "is disable" } ) ;
782+ }
783+ const [ action , details ] = request . params ;
784+ let fs : FileSystem ;
785+ const baseDir = `ScriptCat/app/${ request . script . uuid } ` ;
786+ try {
787+ fs = await FileSystemFactory . create (
788+ config . filesystem ,
789+ config . params [ config . filesystem ]
790+ ) ;
791+ await FileSystemFactory . mkdirAll ( fs , baseDir ) ;
792+ } catch ( e : any ) {
793+ return channel . throw ( { code : 2 , error : e . message } ) ;
794+ }
795+ switch ( action ) {
796+ case "list" :
797+ fs . list ( `${ baseDir } /${ details . path } ` )
798+ . then ( ( list ) => {
799+ list . forEach ( ( file ) => {
800+ ( < any > file ) . absPath = file . path ;
801+ file . path = file . path . substring ( baseDir . length + 1 ) ;
802+ } ) ;
803+ channel . send ( { action : "onload" , data : list } ) ;
804+ channel . disChannel ( ) ;
805+ } )
806+ . catch ( ( e ) => {
807+ channel . throw ( { code : 3 , error : e . message } ) ;
808+ } ) ;
809+ break ;
810+ case "upload" :
811+ // eslint-disable-next-line no-case-declarations
812+ const w = await fs . create ( `${ baseDir } /${ details . path } ` ) ;
813+ w . write ( await ( await fetch ( < string > details . data ) ) . blob ( ) )
814+ . then ( ( ) => {
815+ channel . send ( { action : "onload" , data : true } ) ;
816+ channel . disChannel ( ) ;
817+ } )
818+ . catch ( ( e ) => {
819+ channel . throw ( { code : 4 , error : e . message } ) ;
820+ } ) ;
821+ break ;
822+ case "download" :
823+ // eslint-disable-next-line no-case-declarations, no-undef
824+ const info = < CATType . FileStorageFileInfo > details . file ;
825+ fs = await fs . openDir ( `${ baseDir } ${ info . path } ` ) ;
826+ // eslint-disable-next-line no-case-declarations
827+ const r = await fs . open ( {
828+ fsid : ( < any > info ) . fsid ,
829+ name : info . name ,
830+ path : `${ baseDir } /${ info . path } ` ,
831+ size : info . size ,
832+ digest : info . digest ,
833+ createtime : info . createtime ,
834+ updatetime : info . updatetime ,
835+ } ) ;
836+ r . read ( "blob" )
837+ . then ( ( blob ) => {
838+ const url = URL . createObjectURL ( blob ) ;
839+ setTimeout ( ( ) => {
840+ URL . revokeObjectURL ( url ) ;
841+ } , 6000 ) ;
842+ channel . send ( { action : "onload" , data : url } ) ;
843+ channel . disChannel ( ) ;
844+ } )
845+ . catch ( ( e ) => {
846+ channel . throw ( { code : 5 , error : e . message } ) ;
847+ } ) ;
848+ break ;
849+ case "delete" :
850+ fs . delete ( `${ baseDir } /${ details . path } ` )
851+ . then ( ( ) => {
852+ channel . send ( { action : "onload" , data : true } ) ;
853+ channel . disChannel ( ) ;
854+ } )
855+ . catch ( ( e ) => {
856+ channel . throw ( { code : 6 , error : e . message } ) ;
857+ } ) ;
858+ break ;
859+ default :
860+ channel . disChannel ( ) ;
861+ break ;
862+ }
863+ }
758864}
0 commit comments