2020-12-04 06:45:20 +13:00
|
|
|
const { walkDir } = require("../../../utilities")
|
|
|
|
const { join } = require("../../../utilities/centralPath")
|
|
|
|
const { budibaseAppsDir } = require("../../../utilities/budibaseDir")
|
2020-12-19 01:54:20 +13:00
|
|
|
const fetch = require("node-fetch")
|
2020-12-04 06:45:20 +13:00
|
|
|
const PouchDB = require("../../../db")
|
2020-12-17 08:50:02 +13:00
|
|
|
const CouchDB = require("pouchdb")
|
2021-03-23 07:06:10 +13:00
|
|
|
const { upload } = require("../../../utilities/fileSystem")
|
2020-12-02 02:39:34 +13:00
|
|
|
|
2020-12-17 08:50:02 +13:00
|
|
|
exports.fetchCredentials = async function(url, body) {
|
|
|
|
const response = await fetch(url, {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify(body),
|
2021-01-07 05:58:29 +13:00
|
|
|
headers: { "Content-Type": "application/json" },
|
2020-12-17 08:50:02 +13:00
|
|
|
})
|
|
|
|
|
|
|
|
const json = await response.json()
|
|
|
|
if (json.errors) {
|
|
|
|
throw new Error(json.errors)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.status !== 200) {
|
|
|
|
throw new Error(
|
2021-01-07 05:58:29 +13:00
|
|
|
`Error fetching temporary credentials: ${JSON.stringify(json)}`
|
2020-12-17 08:50:02 +13:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
2021-03-23 07:06:10 +13:00
|
|
|
exports.prepareUpload = async function({ s3Key, bucket, metadata, file }) {
|
|
|
|
const response = await upload({
|
|
|
|
bucket,
|
|
|
|
metadata,
|
|
|
|
filename: s3Key,
|
|
|
|
path: file.path,
|
|
|
|
type: file.type,
|
|
|
|
})
|
2020-12-02 02:39:34 +13:00
|
|
|
|
|
|
|
return {
|
|
|
|
size: file.size,
|
|
|
|
name: file.name,
|
2021-03-23 07:06:10 +13:00
|
|
|
extension: [...file.name.split(".")].pop(),
|
|
|
|
url: response.Location,
|
|
|
|
key: response.Key,
|
2020-12-02 02:39:34 +13:00
|
|
|
}
|
|
|
|
}
|
2020-12-04 06:45:20 +13:00
|
|
|
|
2021-03-23 07:06:10 +13:00
|
|
|
exports.deployToObjectStore = async function(appId, bucket, metadata) {
|
2020-12-04 06:45:20 +13:00
|
|
|
const appAssetsPath = join(budibaseAppsDir(), appId, "public")
|
|
|
|
|
|
|
|
let uploads = []
|
|
|
|
|
2020-12-09 02:45:53 +13:00
|
|
|
// Upload HTML, CSS and JS for each page of the web app
|
|
|
|
walkDir(appAssetsPath, function(filePath) {
|
|
|
|
const filePathParts = filePath.split("/")
|
|
|
|
const appAssetUpload = exports.prepareUpload({
|
2021-03-23 07:06:10 +13:00
|
|
|
bucket,
|
2020-12-09 02:45:53 +13:00
|
|
|
file: {
|
|
|
|
path: filePath,
|
|
|
|
name: filePathParts.pop(),
|
|
|
|
},
|
|
|
|
s3Key: filePath.replace(appAssetsPath, `assets/${appId}`),
|
|
|
|
metadata,
|
2020-12-04 06:45:20 +13:00
|
|
|
})
|
2020-12-09 02:45:53 +13:00
|
|
|
uploads.push(appAssetUpload)
|
|
|
|
})
|
2020-12-04 06:45:20 +13:00
|
|
|
|
|
|
|
// Upload file attachments
|
|
|
|
const db = new PouchDB(appId)
|
|
|
|
let fileUploads
|
|
|
|
try {
|
|
|
|
fileUploads = await db.get("_local/fileuploads")
|
|
|
|
} catch (err) {
|
|
|
|
fileUploads = { _id: "_local/fileuploads", uploads: [] }
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let file of fileUploads.uploads) {
|
|
|
|
if (file.uploaded) continue
|
|
|
|
|
|
|
|
const attachmentUpload = exports.prepareUpload({
|
|
|
|
file,
|
|
|
|
s3Key: `assets/${appId}/attachments/${file.processedFileName}`,
|
2021-03-23 07:06:10 +13:00
|
|
|
bucket,
|
2020-12-04 06:45:20 +13:00
|
|
|
metadata,
|
|
|
|
})
|
|
|
|
|
|
|
|
uploads.push(attachmentUpload)
|
|
|
|
|
|
|
|
// mark file as uploaded
|
|
|
|
file.uploaded = true
|
|
|
|
}
|
|
|
|
|
|
|
|
db.put(fileUploads)
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await Promise.all(uploads)
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error uploading budibase app assets to s3", err)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2020-12-05 06:10:01 +13:00
|
|
|
|
2020-12-17 08:50:02 +13:00
|
|
|
exports.performReplication = (appId, session, dbUrl) => {
|
2020-12-05 06:10:01 +13:00
|
|
|
return new Promise((resolve, reject) => {
|
2020-12-17 08:50:02 +13:00
|
|
|
const local = new PouchDB(appId)
|
|
|
|
|
|
|
|
const remote = new CouchDB(`${dbUrl}/${appId}`, {
|
|
|
|
fetch: function(url, opts) {
|
|
|
|
opts.headers.set("Cookie", `${session};`)
|
|
|
|
return CouchDB.fetch(url, opts)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2020-12-05 06:10:01 +13:00
|
|
|
const replication = local.sync(remote)
|
|
|
|
|
|
|
|
replication.on("complete", () => resolve())
|
|
|
|
replication.on("error", err => reject(err))
|
|
|
|
})
|
|
|
|
}
|