@@ -30,6 +30,9 @@ import { DeleteNoderedInstanceMessage } from "./DeleteNoderedInstanceMessage";
3030import { GetNoderedInstanceMessage } from "./GetNoderedInstanceMessage" ;
3131import { GetNoderedInstanceLogMessage } from "./GetNoderedInstanceLogMessage" ;
3232import { Util } from "../Util" ;
33+ import { SaveFileMessage } from "./SaveFileMessage" ;
34+ import { Readable , Stream } from "stream" ;
35+ import { GridFSBucket } from "mongodb" ;
3336
3437export class Message {
3538 public id : string ;
@@ -150,6 +153,9 @@ export class Message {
150153 case "stopnoderedinstance" :
151154 this . StopNoderedInstance ( cli ) ;
152155 break ;
156+ case "savefile" :
157+ this . SaveFile ( cli ) ;
158+ break ;
153159 default :
154160 this . UnknownCommand ( cli ) ;
155161 break ;
@@ -941,6 +947,79 @@ export class Message {
941947 this . Send ( cli ) ;
942948 }
943949
950+ private async _SaveFile ( stream : Stream , filename : string , contentType : string , metadata : Base ) : Promise < string > {
951+ return new Promise < string > ( async ( resolve , reject ) => {
952+ try {
953+ var bucket = new GridFSBucket ( Config . db . db ) ;
954+ let uploadStream = bucket . openUploadStream ( filename , { contentType : contentType , metadata : metadata } ) ;
955+ let id = uploadStream . id ;
956+ stream . pipe ( uploadStream ) ;
957+ uploadStream . on ( 'error' , function ( error ) {
958+ reject ( error ) ;
959+ } ) .
960+ on ( 'finish' , function ( ) {
961+ resolve ( id . toString ( ) ) ;
962+ } ) ;
963+ } catch ( err ) {
964+ reject ( err ) ;
965+ }
966+ } ) ;
967+ }
968+ private async SaveFile ( cli : WebSocketClient ) : Promise < void > {
969+ this . Reply ( ) ;
970+ var msg : SaveFileMessage
971+ try {
972+ msg = SaveFileMessage . assign ( this . data ) ;
973+ if ( Util . IsNullEmpty ( msg . jwt ) ) { msg . jwt = cli . jwt ; }
974+ if ( Util . IsNullEmpty ( msg . filename ) ) throw new Error ( "Filename is mandatory" ) ;
975+ if ( Util . IsNullEmpty ( msg . mimeType ) ) throw new Error ( "mimeTypes is mandatory" ) ;
976+ if ( Util . IsNullEmpty ( msg . file ) ) throw new Error ( "file is mandatory" ) ;
977+
978+ var buf = Buffer . from ( msg . file , 'base64' ) ;
979+ var readable = new Readable ( ) ;
980+ readable . _read = ( ) => { } ; // _read is required but you can noop it
981+ readable . push ( buf ) ;
982+ readable . push ( null ) ;
983+ if ( msg . metadata == null ) { msg . metadata = new Base ( ) ; }
984+ msg . metadata = Base . assign ( msg . metadata ) ;
985+ if ( Util . IsNullUndefinded ( msg . metadata . _acl ) ) { msg . metadata . _acl = [ ] ; }
986+ var user : TokenUser = Crypt . verityToken ( msg . jwt ) ;
987+ if ( ! Config . db . hasAuthorization ( user , msg . metadata , "create" ) ) { throw new Error ( "Access denied" ) ; }
988+ msg . metadata . _createdby = user . name ;
989+ msg . metadata . _createdbyid = user . _id ;
990+ msg . metadata . _created = new Date ( new Date ( ) . toISOString ( ) ) ;
991+ msg . metadata . _modifiedby = user . name ;
992+ msg . metadata . _modifiedbyid = user . _id ;
993+ msg . metadata . _modified = msg . metadata . _created ;
994+ if ( Util . IsNullEmpty ( msg . metadata . name ) ) {
995+ msg . metadata . name = msg . filename ;
996+ }
997+ var hasUser : any = msg . metadata . _acl . find ( e => e . _id === user . _id ) ;
998+ if ( ( hasUser === null || hasUser === undefined ) ) {
999+ msg . metadata . addRight ( user . _id , user . name , [ Rights . full_control ] ) ;
1000+ }
1001+ hasUser = msg . metadata . _acl . find ( e => e . _id === WellknownIds . filestore_admins ) ;
1002+ if ( ( hasUser === null || hasUser === undefined ) ) {
1003+ msg . metadata . addRight ( WellknownIds . filestore_admins , "filestore admins" , [ Rights . full_control ] ) ;
1004+ }
1005+ hasUser = msg . metadata . _acl . find ( e => e . _id === WellknownIds . filestore_users ) ;
1006+ if ( ( hasUser === null || hasUser === undefined ) ) {
1007+ msg . metadata . addRight ( WellknownIds . filestore_users , "filestore users" , [ Rights . read ] ) ;
1008+ }
1009+ msg . id = await this . _SaveFile ( readable , msg . filename , msg . mimeType , msg . metadata ) ;
1010+ } catch ( error ) {
1011+ if ( Util . IsNullUndefinded ( msg ) ) { ( msg as any ) = { } ; }
1012+ msg . error = error . toString ( ) ;
1013+ cli . _logger . error ( error ) ;
1014+ }
1015+ try {
1016+ this . data = JSON . stringify ( msg ) ;
1017+ } catch ( error ) {
1018+ this . data = "" ;
1019+ cli . _logger . error ( error ) ;
1020+ }
1021+ this . Send ( cli ) ;
1022+ }
9441023}
9451024
9461025export class JSONfn {
0 commit comments