Skip to content

Commit 2ff323d

Browse files
committed
Add endpoint to get collection icon
1 parent 84f2373 commit 2ff323d

File tree

2 files changed

+45
-40
lines changed

2 files changed

+45
-40
lines changed

lib/actions/updateCollectionIcon.js

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const fs = require('fs')
33
const config = require('../config')
44
const extend = require('xtend')
5+
const path = require('path')
56

67
const getOwnedBy = require('../query/ownedBy')
78

@@ -13,41 +14,48 @@ const mkdirp = require('mkdirp')
1314

1415
module.exports = function (req, res) {
1516
const { graphUri, uri, url } = getUrisFromReq(req, res)
16-
return getOwnedBy(uri, graphUri).then((ownedBy) => {
17-
if (ownedBy.indexOf(config.get('databasePrefix') + 'user/' + req.user.username) === -1) {
17+
if (req.method === 'GET') {
18+
const collectionIcons = config.get('collectionIcons')
19+
const iconFile = collectionIcons[uri]
20+
const dir = path.resolve(__dirname, '../../public')
21+
res.sendFile(iconFile, { root: dir })
22+
} else {
23+
return getOwnedBy(uri, graphUri).then((ownedBy) => {
24+
if (ownedBy.indexOf(config.get('databasePrefix') + 'user/' + req.user.username) === -1) {
25+
if (!req.accepts('text/html')) {
26+
res.status(403).type('text/plain').send('Not authorized to update this collection icon')
27+
} else {
28+
const locals = {
29+
config: config.get(),
30+
section: 'errors',
31+
user: req.user,
32+
errors: [ 'Not authorized to update this collection icon' ]
33+
}
34+
res.status(403).send(pug.renderFile('templates/views/errors/errors.jade', locals))
35+
}
36+
}
37+
38+
if (req.file) {
39+
var iconFile = req.file
40+
41+
var collectionIcons = config.get('collectionIcons')
42+
const iconFilename = 'public/icons/' + iconFile.originalname
43+
44+
mkdirp('public/icons').then(() => {
45+
fs.writeFileSync(iconFilename, iconFile.buffer)
46+
})
47+
48+
collectionIcons = extend(collectionIcons, {
49+
[uri]: '/icons/' + iconFile.originalname
50+
})
51+
52+
config.set('collectionIcons', collectionIcons)
53+
}
1854
if (!req.accepts('text/html')) {
19-
res.status(403).type('text/plain').send('Not authorized to update this collection icon')
55+
res.status(200).type('text/plain').send('Success')
2056
} else {
21-
const locals = {
22-
config: config.get(),
23-
section: 'errors',
24-
user: req.user,
25-
errors: [ 'Not authorized to update this collection icon' ]
26-
}
27-
res.status(403).send(pug.renderFile('templates/views/errors/errors.jade', locals))
57+
res.redirect(url)
2858
}
29-
}
30-
31-
if (req.file) {
32-
var iconFile = req.file
33-
34-
var collectionIcons = config.get('collectionIcons')
35-
const iconFilename = 'public/icons/' + iconFile.originalname
36-
37-
mkdirp('public/icons').then(() => {
38-
fs.writeFileSync(iconFilename, iconFile.buffer)
39-
})
40-
41-
collectionIcons = extend(collectionIcons, {
42-
[uri]: '/icons/' + iconFile.originalname
43-
})
44-
45-
config.set('collectionIcons', collectionIcons)
46-
}
47-
if (!req.accepts('text/html')) {
48-
res.status(200).type('text/plain').send('Success')
49-
} else {
50-
res.redirect(url)
51-
}
52-
})
59+
})
60+
}
5361
}

lib/app.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,9 @@ function App () {
327327
app.post('/sparql', requirePublicLogin, bodyParser.urlencoded({ extended: true }), sparql)
328328

329329
// Manage Submissions Endpoints
330-
app.post(
331-
'/public/:collectionId/:displayId/:version/icon',
332-
requireUser,
333-
uploadToMemory.single('collectionIcon'),
334-
actions.updateCollectionIcon
335-
)
330+
app.get('/public/:collectionId/:displayId/:version/icon', actions.updateCollectionIcon)
331+
app.post('/public/:collectionId/:displayId/:version/icon', requireUser, uploadToMemory.single('collectionIcon'),
332+
actions.updateCollectionIcon)
336333

337334
app.get('/public/:collectionId/:displayId/:version/removeCollection', requireAdmin, actions.removeCollection)
338335
app.get('/public/:collectionId/:displayId/:version/remove', requireAdmin, actions.remove)

0 commit comments

Comments
 (0)