forked from tsai/budibase
8 changed files with 216 additions and 155 deletions
@ -0,0 +1,66 @@ |
|||
const { getAppQuota } = require("./quota") |
|||
const env = require("../../../environment") |
|||
|
|||
/** |
|||
* This is used to pass around information about the deployment that is occurring |
|||
*/ |
|||
class Deployment { |
|||
constructor(id, appId) { |
|||
this._id = id |
|||
this.appId = appId |
|||
} |
|||
|
|||
// purely so that we can do quota stuff outside the main deployment context
|
|||
async init() { |
|||
if (!env.SELF_HOSTED) { |
|||
this.setQuota(await getAppQuota(this.appId)) |
|||
} |
|||
} |
|||
|
|||
setQuota(quota) { |
|||
this.quota = quota |
|||
} |
|||
|
|||
getQuota() { |
|||
return this.quota |
|||
} |
|||
|
|||
getAppId() { |
|||
return this.appId |
|||
} |
|||
|
|||
setVerification(verification) { |
|||
this.verification = verification |
|||
} |
|||
|
|||
getVerification() { |
|||
return this.verification |
|||
} |
|||
|
|||
setStatus(status, err = null) { |
|||
this.status = status |
|||
if (err) { |
|||
this.err = err |
|||
} |
|||
} |
|||
|
|||
getJSON() { |
|||
const obj = { |
|||
_id: this._id, |
|||
appId: this.appId, |
|||
status: this.status, |
|||
} |
|||
if (this.err) { |
|||
obj.err = this.err |
|||
} |
|||
if (this.verification && this.verification.cfDistribution) { |
|||
obj.cfDistribution = this.verification.cfDistribution |
|||
} |
|||
if (this.verification && this.verification.quota) { |
|||
obj.quota = this.verification.quota |
|||
} |
|||
return obj |
|||
} |
|||
} |
|||
|
|||
module.exports = Deployment |
|||
@ -0,0 +1,27 @@ |
|||
const PouchDB = require("../../../db") |
|||
const { DocumentTypes, SEPARATOR, UNICODE_MAX } = require("../../../db/utils") |
|||
|
|||
exports.getAppQuota = async function(appId) { |
|||
const db = new PouchDB(appId) |
|||
|
|||
const rows = await db.allDocs({ |
|||
startkey: DocumentTypes.ROW + SEPARATOR, |
|||
endkey: DocumentTypes.ROW + SEPARATOR + UNICODE_MAX, |
|||
}) |
|||
|
|||
const users = await db.allDocs({ |
|||
startkey: DocumentTypes.USER + SEPARATOR, |
|||
endkey: DocumentTypes.USER + SEPARATOR + UNICODE_MAX, |
|||
}) |
|||
|
|||
const existingRows = rows.rows.length |
|||
const existingUsers = users.rows.length |
|||
|
|||
const designDoc = await db.get("_design/database") |
|||
|
|||
return { |
|||
rows: existingRows, |
|||
users: existingUsers, |
|||
views: Object.keys(designDoc.views).length, |
|||
} |
|||
} |
|||
@ -0,0 +1,5 @@ |
|||
exports.preDeployment = async function(deployment) {} |
|||
|
|||
exports.postDeployment = async function(deployment) {} |
|||
|
|||
exports.deploy = async function(deployment) {} |
|||
@ -0,0 +1,31 @@ |
|||
const fs = require("fs") |
|||
const sanitize = require("sanitize-s3-objectkey") |
|||
|
|||
const CONTENT_TYPE_MAP = { |
|||
html: "text/html", |
|||
css: "text/css", |
|||
js: "application/javascript", |
|||
} |
|||
|
|||
exports.prepareUpload = async function({ s3Key, metadata, s3, file }) { |
|||
const extension = [...file.name.split(".")].pop() |
|||
const fileBytes = fs.readFileSync(file.path) |
|||
|
|||
const upload = await s3 |
|||
.upload({ |
|||
// windows file paths need to be converted to forward slashes for s3
|
|||
Key: sanitize(s3Key).replace(/\\/g, "/"), |
|||
Body: fileBytes, |
|||
ContentType: file.type || CONTENT_TYPE_MAP[extension.toLowerCase()], |
|||
Metadata: metadata, |
|||
}) |
|||
.promise() |
|||
|
|||
return { |
|||
size: file.size, |
|||
name: file.name, |
|||
extension, |
|||
url: upload.Location, |
|||
key: upload.Key, |
|||
} |
|||
} |
|||
Loading…
Reference in new issue