Skip to content

Commit

Permalink
Merge branch 'master' into fixRemoveOwner
Browse files Browse the repository at this point in the history
  • Loading branch information
cjmyers authored Jan 29, 2025
2 parents f66f6b2 + 544350e commit 8245e0b
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 12 deletions.
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
22 changes: 21 additions & 1 deletion lib/views/addToCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const pug = require('pug')
const config = require('../config')
const sparql = require('../sparql/sparql')
const getOwnedBy = require('../query/ownedBy')
const getUrisFromReq = require('../getUrisFromReq')

module.exports = function (req, res) {
req.setTimeout(0) // no timeout
Expand All @@ -18,11 +19,16 @@ module.exports = function (req, res) {
}
}

const { uri } = getUrisFromReq(req, res)

return sparql.queryJson(collectionQuery, req.user.graphUri).then((collections) => {
collections.map((result) => {
collections = collections.filter((result) => {
return result.subject !== uri
}).map((result) => {
result.uri = result.subject
result.name = result.name ? result.name : result.uri.toString()
delete result.subject
return result
})
collections.sort(sortByNames)

Expand Down Expand Up @@ -64,6 +70,20 @@ module.exports = function (req, res) {
memberUri = memberUri.replace('/user/', config.get('databasePrefix') + 'user/')
}

if (memberUri === uri) {
if (!req.accepts('text/html')) {
return res.status(400).type('text/plain').send('Cannot make a collection a member of itself')
} else {
const locals = {
config: config.get(),
section: 'errors',
user: req.user,
errors: [ 'Cannot make a collection a member of itself' ]
}
return res.status(400).send(pug.renderFile('templates/views/errors/errors.jade', locals))
}
}

var templateParams = {
uri: uri,
memberUri: memberUri
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

0 comments on commit 8245e0b

Please sign in to comment.