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,48 @@ 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 ) {
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}
0 commit comments