@ -1,17 +1,10 @@
const fs = require ( "fs" )
const sanitize = require ( "sanitize-s3-objectkey" )
const { walkDir } = require ( "../../../utilities" )
const { walkDir } = require ( "../../../utilities" )
const { join } = require ( "../../../utilities/centralPath" )
const { join } = require ( "../../../utilities/centralPath" )
const { budibaseAppsDir } = require ( "../../../utilities/budibaseDir" )
const { budibaseAppsDir } = require ( "../../../utilities/budibaseDir" )
const fetch = require ( "node-fetch" )
const fetch = require ( "node-fetch" )
const PouchDB = require ( "../../../db" )
const PouchDB = require ( "../../../db" )
const CouchDB = require ( "pouchdb" )
const CouchDB = require ( "pouchdb" )
const { upload } = require ( "../../../utilities/fileSystem" )
const CONTENT_TYPE_MAP = {
html : "text/html" ,
css : "text/css" ,
js : "application/javascript" ,
}
exports . fetchCredentials = async function ( url , body ) {
exports . fetchCredentials = async function ( url , body ) {
const response = await fetch ( url , {
const response = await fetch ( url , {
@ -34,30 +27,25 @@ exports.fetchCredentials = async function(url, body) {
return json
return json
}
}
exports . prepareUpload = async function ( { s3Key , metadata , client , file } ) {
exports . prepareUpload = async function ( { s3Key , bucket , metadata , file } ) {
const extension = [ ... file . name . split ( "." ) ] . pop ( )
const response = await upload ( {
const fileBytes = fs . readFileSync ( file . path )
bucket ,
metadata ,
const upload = await client
filename : s3Key ,
. upload ( {
path : file . path ,
// windows file paths need to be converted to forward slashes for s3
type : file . type ,
Key : sanitize ( s3Key ) . replace ( /\\/g , "/" ) ,
} )
Body : fileBytes ,
ContentType : file . type || CONTENT_TYPE_MAP [ extension . toLowerCase ( ) ] ,
Metadata : metadata ,
} )
. promise ( )
return {
return {
size : file . size ,
size : file . size ,
name : file . name ,
name : file . name ,
extension ,
extension : [ ... file . name . split ( "." ) ] . pop ( ) ,
url : upload . Location ,
url : response . Location ,
key : upload . Key ,
key : response . Key ,
}
}
}
}
exports . deployToObjectStore = async function ( appId , objectClien t, metadata ) {
exports . deployToObjectStore = async function ( appId , bucke t, metadata ) {
const appAssetsPath = join ( budibaseAppsDir ( ) , appId , "public" )
const appAssetsPath = join ( budibaseAppsDir ( ) , appId , "public" )
let uploads = [ ]
let uploads = [ ]
@ -66,12 +54,12 @@ exports.deployToObjectStore = async function(appId, objectClient, metadata) {
walkDir ( appAssetsPath , function ( filePath ) {
walkDir ( appAssetsPath , function ( filePath ) {
const filePathParts = filePath . split ( "/" )
const filePathParts = filePath . split ( "/" )
const appAssetUpload = exports . prepareUpload ( {
const appAssetUpload = exports . prepareUpload ( {
bucket ,
file : {
file : {
path : filePath ,
path : filePath ,
name : filePathParts . pop ( ) ,
name : filePathParts . pop ( ) ,
} ,
} ,
s3Key : filePath . replace ( appAssetsPath , ` assets/ ${ appId } ` ) ,
s3Key : filePath . replace ( appAssetsPath , ` assets/ ${ appId } ` ) ,
client : objectClient ,
metadata ,
metadata ,
} )
} )
uploads . push ( appAssetUpload )
uploads . push ( appAssetUpload )
@ -92,7 +80,7 @@ exports.deployToObjectStore = async function(appId, objectClient, metadata) {
const attachmentUpload = exports . prepareUpload ( {
const attachmentUpload = exports . prepareUpload ( {
file ,
file ,
s3Key : ` assets/ ${ appId } /attachments/ ${ file . processedFileName } ` ,
s3Key : ` assets/ ${ appId } /attachments/ ${ file . processedFileName } ` ,
client : objectClien t,
bucke t,
metadata ,
metadata ,
} )
} )