Skip to content

Commit

Permalink
Data export: added extension to exported files (#3028)
Browse files Browse the repository at this point in the history
Co-authored-by: Stefano Ricci <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 21, 2023
1 parent c6cce22 commit f5b4cd9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 3 additions & 2 deletions core/record/recordFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ export const createFile = (uuid, fileName, fileSize, content, recordUuid, nodeUu
[keys.content]: content,
})

const getExtension = (fileName) => R.pipe(R.split('.'), R.tail)(fileName)
const getExtensionFromFileName = (fileName) => R.pipe(R.split('.'), R.tail)(fileName)

export const truncateFileName = (fileName, maxLength = 10) => {
if (fileName && !R.isEmpty(fileName)) {
const extension = getExtension(fileName)
const extension = getExtensionFromFileName(fileName)

return R.pipe(R.dropLast(extension.length + 1), truncate(maxLength), (name) => `${name}.${extension}`)(fileName)
}
Expand All @@ -46,6 +46,7 @@ export const isDeleted = (file) => Boolean(ObjectUtils.getProp(propKeys.deleted,
export const getName = ObjectUtils.getProp(propKeys.name)
export const getSize = ObjectUtils.getProp(propKeys.size)
export const getContent = R.prop(keys.content)
export const getExtension = R.pipe(getName, getExtensionFromFileName)

// UPDATE
export const assocContent = R.assoc(keys.content)
7 changes: 6 additions & 1 deletion server/modules/survey/service/export/jobs/FilesExportJob.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as PromiseUtils from '@core/promiseUtils'
import * as Survey from '@core/survey/survey'
import * as RecordFile from '@core/record/recordFile'

import Job from '@server/job/job'
import * as FileUtils from '@server/utils/file/fileUtils'

import * as SurveyRdbService from '@server/modules/surveyRdb/service/surveyRdbService'
import * as FileService from '@server/modules/record/service/fileService'
import { Objects } from '@openforis/arena-core'

export default class FilesExportJob extends Job {
constructor(params) {
Expand All @@ -31,12 +33,15 @@ export default class FilesExportJob extends Job {
// write the files in subfolders by cycle
await PromiseUtils.each(Object.entries(fileUuidsByCycle), async ([cycle, fileUuids]) => {
await PromiseUtils.each(fileUuids, async (fileUuid) => {
const fileSummary = await FileService.fetchFileSummaryByUuid(surveyId, fileUuid, this.tx)
const recordFileContent = await FileService.fetchFileContentAsStream({ surveyId, fileUuid }, this.tx)
const cycleFilesPath = FileUtils.join(outputDir, 'files', cycle)
if (!FileUtils.exists(cycleFilesPath)) {
await FileUtils.mkdir(cycleFilesPath)
}
const tempFilePath = FileUtils.join(cycleFilesPath, fileUuid)
const extension = RecordFile.getExtension(fileSummary)
const exportedFileName = Objects.isEmpty(extension) ? fileUuid : `${fileUuid}.${extension}`
const tempFilePath = FileUtils.join(cycleFilesPath, exportedFileName)
await FileUtils.writeFile(tempFilePath, recordFileContent)
this.incrementProcessedItems()
})
Expand Down

0 comments on commit f5b4cd9

Please sign in to comment.