lint, tidy up and some simplification

This commit is contained in:
Martin McKeaveney 2020-09-23 17:29:32 +01:00
parent 938f675922
commit 329e885ab6
4 changed files with 51 additions and 80 deletions

View File

@ -28,7 +28,7 @@ export const getBackendUiStore = () => {
}, },
}, },
records: { records: {
save: record => save: () =>
store.update(state => { store.update(state => {
state.selectedView = state.selectedView state.selectedView = state.selectedView
return state return state

View File

@ -31,8 +31,6 @@ exports.uploadFile = async function(ctx) {
? Array.from(ctx.request.files.file) ? Array.from(ctx.request.files.file)
: [ctx.request.files.file] : [ctx.request.files.file]
let uploads = []
const attachmentsPath = resolve( const attachmentsPath = resolve(
budibaseAppsDir(), budibaseAppsDir(),
ctx.user.appId, ctx.user.appId,
@ -47,7 +45,7 @@ exports.uploadFile = async function(ctx) {
}, },
}) })
uploads = files.map(file => { const uploads = files.map(file => {
const fileExtension = [...file.name.split(".")].pop() const fileExtension = [...file.name.split(".")].pop()
const processedFileName = `${uuid.v4()}.${fileExtension}` const processedFileName = `${uuid.v4()}.${fileExtension}`
@ -59,95 +57,47 @@ exports.uploadFile = async function(ctx) {
s3, s3,
}) })
}) })
} else {
uploads = processLocalFileUploads(files, attachmentsPath)
// uploads = files.map(file => {
// const fileExtension = [...file.name.split(".")].pop()
// const processedFileName = `${uuid.v4()}.${fileExtension}`
// return fileProcessor.process({ ctx.body = await Promise.all(uploads)
// format: file.format, return
// type: file.type,
// name: file.name,
// size: file.size,
// path: file.path,
// processedFileName,
// extension: fileExtension,
// outputPath: `${attachmentsPath}/${processedFileName}`,
// url: `/attachments/${processedFileName}`,
// })
// })
} }
const responses = await Promise.all(uploads) ctx.body = await processLocalFileUploads({
files,
ctx.body = responses outputPath: attachmentsPath,
instanceId: ctx.user.instanceId,
})
} }
function processLocalFileUploads(files, attachmentsPath) { async function processLocalFileUploads({ files, outputPath, instanceId }) {
// create attachments dir if it doesnt exist // create attachments dir if it doesnt exist
!fs.existsSync(attachmentsPath) && !fs.existsSync(outputPath) && fs.mkdirSync(outputPath, { recursive: true })
fs.mkdirSync(attachmentsPath, { recursive: true })
const filesToProcess = files.map(file => { const filesToProcess = files.map(file => {
const fileExtension = [...file.name.split(".")].pop() const fileExtension = [...file.name.split(".")].pop()
// filenames converted to UUIDs so they are unique // filenames converted to UUIDs so they are unique
const processedFileName = `${uuid.v4()}.${fileExtension}` const processedFileName = `${uuid.v4()}.${fileExtension}`
console.log(file)
return { return {
...file, ...file,
processedFileName, processedFileName,
extension: fileExtension, extension: fileExtension,
outputPath: join(attachmentsPath, processedFileName), outputPath: join(outputPath, processedFileName),
url: join("/attachments", processedFileName), url: join("/attachments", processedFileName),
} }
}) })
return filesToProcess.map(fileProcessor.process) const fileProcessOperations = filesToProcess.map(fileProcessor.process)
}
exports.performLocalFileProcessing = async function(ctx) {
const { files } = ctx.request.body
const processedFileOutputPath = resolve(
budibaseAppsDir(),
ctx.user.appId,
"attachments"
)
const fileProcessOperations = processLocalFileUploads(
files,
processedFileOutputPath
)
// // create attachments dir if it doesnt exist
// !fs.existsSync(attachmentsPath) &&
// fs.mkdirSync(attachmentsPath, { recursive: true })
// const filesToProcess = files.map(file => {
// const fileExtension = [...file.path.split(".")].pop()
// // filenames converted to UUIDs so they are unique
// const processedFileName = `${uuid.v4()}.${fileExtension}`
// return {
// ...file,
// processedFileName,
// extension: fileExtension,
// outputPath: join(attachmentsPath, processedFileName),
// url: join("/attachments", processedFileName),
// }
// })
// const fileProcessOperations = filesToProcess.map(fileProcessor.process)
try {
const processedFiles = await Promise.all(fileProcessOperations) const processedFiles = await Promise.all(fileProcessOperations)
let pendingFileUploads let pendingFileUploads
// local document used to track which files need to be uploaded // local document used to track which files need to be uploaded
// db.get throws an error if the document doesn't exist // db.get throws an error if the document doesn't exist
// need to use a promise to default // need to use a promise to default
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(instanceId)
await db await db
.get("_local/fileuploads") .get("_local/fileuploads")
.then(data => { .then(data => {
@ -163,7 +113,24 @@ exports.performLocalFileProcessing = async function(ctx) {
] ]
await db.put(pendingFileUploads) await db.put(pendingFileUploads)
ctx.body = processedFiles return processedFiles
}
exports.performLocalFileProcessing = async function(ctx) {
const { files } = ctx.request.body
const processedFileOutputPath = resolve(
budibaseAppsDir(),
ctx.user.appId,
"attachments"
)
try {
ctx.body = await processLocalFileUploads({
files,
outputPath: processedFileOutputPath,
instanceId: ctx.user.instanceId,
})
} catch (err) { } catch (err) {
ctx.throw(500, err) ctx.throw(500, err)
} }

View File

@ -1,6 +1,10 @@
const apiCall = method => async (url, body, headers = { const apiCall = method => async (
url,
body,
headers = {
"Content-Type": "application/json", "Content-Type": "application/json",
}) => { }
) => {
const response = await fetch(url, { const response = await fetch(url, {
method: method, method: method,
body: body && JSON.stringify(body), body: body && JSON.stringify(body),

View File

@ -1,5 +1,5 @@
<script> <script>
import { FILE_TYPES } from "./fileTypes"; import { FILE_TYPES } from "./fileTypes"
export let files export let files
export let height = "70" export let height = "70"