Skip to content

Commit

Permalink
fix: file missing callback (#51)
Browse files Browse the repository at this point in the history
* fix: missing callback

* refactor[file]: do not files on filename changes

* fix: release template scripts when using externally
  • Loading branch information
facugon committed May 12, 2022
1 parent be24099 commit aa6819b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
27 changes: 14 additions & 13 deletions core/lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ const logger = require('./logger')('lib:file')
const storage = require('./storage').get()
const filenameRegexp = /^[A-Za-z0-9._][0-9a-zA-Z-_.]*$/

function isValidFilename (filename) {
if (!filename) { return false }
return filenameRegexp.test(filename)
}

function keynamegen (filename) {
return filename + '[ts:' + Date.now() + ']'
}

module.exports = {
/**
*
Expand All @@ -35,10 +26,10 @@ module.exports = {
}

// filename has changed
if (input.model.filename !== filename) {
logger.log('new file uploaded. removing old one')
storage.remove(input.model)
}
//if (input.model.filename !== filename) {
// logger.log('new file uploaded. removing old one')
// storage.remove(input.model)
//}

storage.save({
filename: keyname,
Expand Down Expand Up @@ -163,3 +154,13 @@ module.exports = {
})
}
}

function isValidFilename (filename) {
if (!filename) { return false }
return filenameRegexp.test(filename)
}

function keynamegen (filename) {
return filename + '[ts:' + Date.now() + ']'
}

2 changes: 2 additions & 0 deletions core/lib/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ const LocalStorage = {
return stream
},
remove (file, next) {
next || (next = (() => {}))

const storagePath = systemConfig.file_upload_folder
try {
const filepath = path.join(storagePath, file.customer_name, 'scripts', file.keyname)
Expand Down
28 changes: 17 additions & 11 deletions core/service/task.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

const App = require('../app')

const ObjectId = require('mongoose').Types.ObjectId
const ObjectID = require('mongoose').Types.ObjectId

const isMongoId = require('validator/lib/isMongoId')
const logger = require('../lib/logger')('service:task')
Expand Down Expand Up @@ -587,9 +587,13 @@ FactoryMethod[ TaskConstants.TYPE_SCRIPT ] = async function (input) {
}

// the script was created or determined in a previous phase.
// no further actions are required. skip this
if (!ObjectId.isValid(input.script_id)) {
let script
// ensure it is an ObjectID instance and not a MongoID
let script
if (input.script_id instanceof ObjectID) {
script = await App.Models.File.File.findById(input.script_id)
}

if (!script) {
if (input.script_id) { // string
script = await App.Models.File.File.findById(input.script_id)
} else if (input.script?.id) { // file model
Expand All @@ -608,23 +612,25 @@ FactoryMethod[ TaskConstants.TYPE_SCRIPT ] = async function (input) {
script = await App.file.create(attrs)
}
}
}

if (script?._id) {
if (script?._id) {
if (script.template_id) {
if (
!task.template_id ||
(task.template_id.toString() !== script.template_id.toString())
(task.template_id.toString() !== script.template_id?.toString())
) {
// remove from the template
script.template = null
script.template_id = null
await script.save()
}
task.script_id = script._id
task.script = script._id
} else {
task.script_id = null
task.script = null
}
task.script_id = script._id
task.script = script._id
} else {
task.script_id = null
task.script = null
}

return task
Expand Down

0 comments on commit aa6819b

Please sign in to comment.