Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes a number of problems with attachments #1607

Merged
merged 9 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/actions/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ module.exports = function (req, res) {
return res.status(401).send('not authorized to edit this submission')
}

let formFields = {} // Object to store parsed form fields

form.on('field', (name, value) => {
// Capture the form fields, such as `id`
formFields[name] = value
})

form.on('part', (partStream) => {
if (partStream.filename) {
uploads.createUpload(partStream).then((uploadInfo) => {
Expand All @@ -41,7 +48,7 @@ module.exports = function (req, res) {

return attachments.addAttachmentToTopLevel(
graphUri, baseUri, uri, partStream.filename, hash, size,
mime, req.user.username)
mime, req.user.username, formFields.id)
}).then(() => {
var templateParams = {
uri: uri
Expand Down
12 changes: 10 additions & 2 deletions lib/api/attachUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ async function serve (req, res) {
let typeString = req.body.type
let name = req.body.name || uuid()

let displayId = name
// Replace non-alphanumeric characters and spaces with underscores
let displayId = name.replace(/[^a-zA-Z0-9]/g, '_')
// Ensure it does not start with a number by prefixing an underscore if needed
if (/^\d/.test(displayId)) {
displayId = '_' + displayId
}

let version = '1'
let attachmentUri = `${baseUri}/${displayId}/${version}`
let persistentIdentity = `${baseUri}/${displayId}`
let ownedBy = await getOwnedBy(uri, graphUri)
const collectionUri = baseUri + baseUri.slice(baseUri.lastIndexOf('/')) + '_collection/' + version

if (ownedBy.indexOf(config.get('databasePrefix') + 'user/' + req.user.username) === -1) {
if (!req.accepts('text/html')) {
Expand Down Expand Up @@ -52,7 +59,8 @@ async function serve (req, res) {
ownedBy: ownedBy,
uri: attachmentUri,
source: source,
type: typeString
type: typeString,
collectionUri: collectionUri
})

console.log('attachUrl: ' + graphUri)
Expand Down
21 changes: 18 additions & 3 deletions lib/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@ const sha1 = require('sha1')
const uuid = require('uuid/v4')
const { fetchSBOLObjectRecursive } = require('./fetch/fetch-sbol-object-recursive')

function addAttachmentToTopLevel (graphUri, baseUri, topLevelUri, name, uploadHash, size, attachmentType, owner) {
// console.log('Adding:'+name+' to:'+topLevelUri)
const displayId = 'attachment_' + uuid().replace(/-/g, '')
function addAttachmentToTopLevel (graphUri, baseUri, topLevelUri, name, uploadHash, size, attachmentType, owner, id) {
// console.log('Adding:'+name+' to:'+topLevelUri)

// Replace non-alphanumeric characters and spaces with underscores
var cleanId = id
if (cleanId) {
cleanId = id.replace(/[^a-zA-Z0-9]/g, '_')
// Ensure it does not start with a number by prefixing an underscore if needed
if (/^\d/.test(cleanId)) {
cleanId = '_' + cleanId
}
}

const displayId = cleanId || 'attachment_' + uuid().replace(/-/g, '')
const persistentIdentity = baseUri + '/' + displayId
// TODO: should get version from topLevelUri
const version = '1'
Expand Down Expand Up @@ -130,6 +141,7 @@ function getAttachmentsFromTopLevel (sbol, topLevel, share) {
type: attachmentType,
image: attachmentIsImage,
url: url,
source: attachment.source.toString(),
size: attachment.size,
sizeString: attachment.size === -1 ? null : filesize(attachment.size)
})
Expand All @@ -143,6 +155,7 @@ function getAttachmentsFromTopLevel (sbol, topLevel, share) {
name: metaData.name,
type: 'unknown',
url: url,
source: 'unknown',
size: -1,
sizeString: 'unknown'
})
Expand Down Expand Up @@ -194,6 +207,7 @@ function getAttachmentsFromList (graphUri, attachmentList, share) {
name: attachment.name,
type: format || 'Other',
url: url,
source: attachment.source.toString(),
size: size,
sizeString: size === -1 ? null : filesize(size)
})
Expand All @@ -204,6 +218,7 @@ function getAttachmentsFromList (graphUri, attachmentList, share) {
name: 'Attachment is missing',
type: 'Other',
url: uri,
source: 'unknown',
size: 0,
sizeString: null
})
Expand Down
7 changes: 3 additions & 4 deletions lib/views/submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,8 @@ async function handleSubmission (req, res, submission) {

await sparql.uploadFile(submissionData.createdBy.graphUri, resultFilename, 'application/rdf+xml')

let baseURI = config.get('databasePrefix') + 'user/' + encodeURIComponent(submissionData.createdBy.username) + '/' + submissionData.collectionId
let collectionURI = baseURI + '/' + submissionData.collectionId + '_collection/' + submissionData.version
let baseURI = config.get('databasePrefix') + 'user/' + encodeURIComponent(submissionData.createdBy.username) + '/' + submissionData.collectionId.replace('_collection', '')
let collectionURI = baseURI + '/' + submissionData.collectionId + '/' + submissionData.version

let sourceQuery = loadTemplate('sparql/GetAttachmentSourceFromTopLevel.sparql', { uri: collectionURI })

Expand Down Expand Up @@ -504,7 +504,6 @@ async function handleSubmission (req, res, submission) {

return
}

let attachmentUri = await attachments.addAttachmentToTopLevel(
submissionData.createdBy.graphUri,
baseURI,
Expand All @@ -513,7 +512,7 @@ async function handleSubmission (req, res, submission) {
hash,
size,
attachmentFiles[originalFilename] || mime,
submissionData.createdBy.username)
submissionData.createdBy.username, null)

console.log(attachmentUri)

Expand Down
2 changes: 2 additions & 0 deletions sparql/AttachUrl.sparql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ PREFIX sbol: <http://sbols.org/v2#>
INSERT {
<$topLevel> sbol:attachment <$uri> .

<$collectionUri> sbol:member <$uri> .

<$uri> a sbol:Attachment ;
dcterms:title "$name" ;
sbol:displayId "$displayId" ;
Expand Down
5 changes: 4 additions & 1 deletion templates/mixins/attachments.jade
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mixin attachments(url, attachments, canEdit)
if attachment.sizeString
| #{attachment.sizeString}
td
a(href=attachment.url + '/download')
a(href=attachment.source)
button.btn.btn-primary
span.fa.fa-download
| Download
Expand All @@ -38,6 +38,9 @@ mixin attachments(url, attachments, canEdit)
span.btn.btn-primary
| Select file(s)&hellip;
input(type="file",name="file",style="display: none;",multiple,form="sbh-attachment-upload")
div.row(style="margin-top: 10px")
div.col-md-6
input.form-control(type="text", name="id", placeholder="Name", form="sbh-attachment-upload")
td(colspan=2)
button.btn.btn-success(type='submit',style='width:100%;',form="sbh-attachment-upload") Attach
div.row
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,11 @@ <h4 class="panel-title">
</span>
</label>
</div>
<div class="row" style="margin-top: 10px">
<div class="col-md-6">
<input class="form-control" form="sbh-attachment-upload" name="id" placeholder="Name" type="text"/>
</div>
</div>
</td>
<td colspan="2">
<button class="btn btn-success" form="sbh-attachment-upload" style="width:100%" type="submit">
Expand Down