11var dirTree = require ( 'directory-tree' ) ,
22 fs = require ( 'fs' ) ,
3+ util = require ( 'util' ) ,
34 mime = require ( 'mime-types' ) ;
45var console = require ( './consoleService' ) ( 'FILE CONTENT' , [ 'blue' , 'inverse' ] ) ;
56
@@ -20,16 +21,24 @@ fileService.get = function(req, res) {
2021
2122 var showNoContent = false ||
2223 isFileOfType ( 'zip' ) ||
23- isFileOfType ( 'program' ) ||
24- isFileOfType ( 'image' ) ||
25- isFileOfType ( 'font' ) ;
24+ isFileOfType ( 'program' ) ;
2625
2726 // temprorary solution until we have a view selector on the FRONT-END
2827 if ( showNoContent ) {
2928 res . json ( {
3029 content : 'awww man... we can\'t show ' + mimeType + ' yet :-(' ,
3130 mimeType : 'text/text'
3231 } ) ;
32+ } else if ( isFileOfType ( 'image' ) ) {
33+ console . info ( 'image requested. Serving data URI' ) ;
34+ var dataUri = base64Image ( fileFullPath ) ;
35+ var file = {
36+ content : dataUri ,
37+ mimeType : mimeType
38+ } ;
39+ res . json ( file ) ;
40+ } else if ( isFileOfType ( 'font' ) ) {
41+ res . download ( fileFullPath ) ; // Set disposition and send it.
3342 } else {
3443 fs . readFile ( fileFullPath , 'utf8' , function ( err , data ) {
3544 if ( err ) {
@@ -119,7 +128,6 @@ fileService.putExtraArg = function(req, res) {
119128 message : 'For a hard save, 2nd param should be: True'
120129 } ) ;
121130 }
122-
123131} ;
124132
125133fileService . getFileTags = function ( filepath ) {
@@ -174,6 +182,11 @@ fileService.getFileTags = function(filepath) {
174182 return fileTags ;
175183} ;
176184
185+ function base64Image ( src ) {
186+ var data = fs . readFileSync ( src ) . toString ( 'base64' ) ;
187+ return util . format ( 'data:%s;base64,%s' , mime . lookup ( src ) , data ) ;
188+ }
189+
177190function extraTypes ( filepath ) {
178191 if ( filepath . indexOf ( '.' ) !== - 1 ) {
179192 var extensionStart = filepath . lastIndexOf ( '.' ) + 1 ;
0 commit comments