Skip to content

Commit df1a868

Browse files
authored
Merge branch 'master' into fixAttachSource
2 parents 9ee8450 + c303165 commit df1a868

File tree

2 files changed

+45
-36
lines changed

2 files changed

+45
-36
lines changed

lib/actions/updateCollectionIcon.js

Lines changed: 42 additions & 30 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,52 @@ 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) {
18-
if (!req.accepts('text/html')) {
19-
res.status(403).type('text/plain').send('Not authorized to update this collection icon')
20-
} else {
21-
const locals = {
22-
config: config.get(),
23-
section: 'errors',
24-
user: req.user,
25-
errors: [ 'Not authorized to update this collection icon' ]
17+
if (req.method === 'GET') {
18+
const collectionIcons = config.get('collectionIcons')
19+
const iconFile = collectionIcons[uri]
20+
if (iconFile) {
21+
const dir = path.resolve(__dirname, '../../public')
22+
res.sendFile(iconFile, { root: dir })
23+
} else {
24+
res.status(403).type('text/plain').send('No icon for this collection')
25+
}
26+
} else {
27+
return getOwnedBy(uri, graphUri).then((ownedBy) => {
28+
if (ownedBy.indexOf(config.get('databasePrefix') + 'user/' + req.user.username) === -1) {
29+
if (!req.accepts('text/html')) {
30+
res.status(403).type('text/plain').send('Not authorized to update this collection icon')
31+
} else {
32+
const locals = {
33+
config: config.get(),
34+
section: 'errors',
35+
user: req.user,
36+
errors: [ 'Not authorized to update this collection icon' ]
37+
}
38+
res.status(403).send(pug.renderFile('templates/views/errors/errors.jade', locals))
2639
}
27-
res.status(403).send(pug.renderFile('templates/views/errors/errors.jade', locals))
2840
}
29-
}
3041

31-
if (req.file) {
32-
var iconFile = req.file
42+
if (req.file) {
43+
var iconFile = req.file
3344

34-
var collectionIcons = config.get('collectionIcons')
35-
const iconFilename = 'public/icons/' + iconFile.originalname
45+
var collectionIcons = config.get('collectionIcons')
46+
const iconFilename = 'public/icons/' + iconFile.originalname
3647

37-
mkdirp('public/icons').then(() => {
38-
fs.writeFileSync(iconFilename, iconFile.buffer)
39-
})
48+
mkdirp('public/icons').then(() => {
49+
fs.writeFileSync(iconFilename, iconFile.buffer)
50+
})
4051

41-
collectionIcons = extend(collectionIcons, {
42-
[uri]: '/icons/' + iconFile.originalname
43-
})
52+
collectionIcons = extend(collectionIcons, {
53+
[uri]: '/icons/' + iconFile.originalname
54+
})
4455

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-
})
56+
config.set('collectionIcons', collectionIcons)
57+
}
58+
if (!req.accepts('text/html')) {
59+
res.status(200).type('text/plain').send('Success')
60+
} else {
61+
res.redirect(url)
62+
}
63+
})
64+
}
5365
}

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)