22const fs = require ( 'fs' )
33const config = require ( '../config' )
44const extend = require ( 'xtend' )
5+ const path = require ( 'path' )
56
67const getOwnedBy = require ( '../query/ownedBy' )
78
@@ -13,41 +14,52 @@ const mkdirp = require('mkdirp')
1314
1415module . 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}
0 commit comments