This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/articles/:uuid/file', upload.array('file', 2), (req, res, next) => { | |
const session = driver.session(); | |
let images = []; | |
let captions = req.body.caption; | |
const sessionUuid = uuidV4(); | |
req.files.forEach((file, index) => { | |
images.push(Object.assign({}, { | |
url: `uploads/articles/${req.params.uuid}/${file.originalname}`, | |
caption: captions[index], | |
name: file.originalname, | |
sessionUuid, | |
})) | |
}) | |
const currentTimestamp = moment().format('YYYY-MM-DD HH:MM:SS'); | |
images.forEach((image, index) => { | |
const tempSession = driver.session(); | |
let writeTxPromise = tempSession.writeTransaction(tx => { | |
tx.run(`MATCH (n:Article {uuid: $uuid}) | |
MERGE (n)-[:HAS]->(i:File { | |
name: $name, | |
caption: $caption | |
}) WITH i,n SET i.uuid = $fileUuid, | |
i.session_uuid = $sessionUuid, | |
i.url = $url, | |
i.updated_at = $updatedAt, | |
i.created_at = $createdAt RETURN n`, | |
{ | |
uuid: req.params.uuid, | |
sessionUuid, | |
fileUuid: uuidV4(), | |
url: image.url, | |
name: image.name, | |
caption: image.caption, | |
createdAt: currentTimestamp, | |
updatedAt: currentTimestamp, | |
}); | |
}); | |
writeTxPromise.then(result => { | |
console.log('File saved.'); | |
tempSession.close(); | |
const isLastImagePromise = (index + 1 === images.length); | |
if (isLastImagePromise) { | |
session.readTransaction(tx => tx.run( | |
`MATCH a=(n:Article {uuid: $uuid}) | |
WHERE SIZE((n)-[:HAS]->()) = 0 | |
WITH COLLECT(a) AS bs CALL apoc.convert.toTree(bs) yield value RETURN value | |
UNION MATCH b=(n:Article)-[:HAS*]->(m) WITH COLLECT(b) AS as CALL apoc.convert.toTree(as) yield value RETURN value`, | |
{ | |
uuid: req.params.uuid, | |
})).then(result => { | |
session.close(); | |
const article = result.records[0]._fields[0]; | |
const savedFiles = article.has || []; | |
const tooManyFiles = savedFiles.length > 2; | |
let responseFiles = savedFiles; | |
if (tooManyFiles) { | |
responseFiles = savedFiles.filter(file => file.session_uuid === sessionUuid); | |
savedFiles.forEach(file => { | |
if (file.session_uuid === sessionUuid) { | |
return; | |
} | |
const cleanupSession = driver.session(); | |
cleanupSession.writeTransaction(tx => tx.run( | |
`MATCH (f:File {uuid: $uuid}) DETACH DELETE f`, | |
{ | |
uuid: file.uuid | |
} | |
)).then(result => { | |
cleanupSession.close(); | |
}).catch(error => console.log(error)); | |
}) | |
} | |
// on application exit: | |
driver.close(); | |
let response = { | |
data: Object.assign({}, article, { | |
has: responseFiles | |
}) | |
}; | |
res.send(response); | |
}); | |
} | |
}).catch(error => { | |
console.log(error); | |
driver.close(); | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment