Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file type on server side #132

Merged
merged 19 commits into from
May 20, 2016
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions app/models/fileService.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
var dirTree = require('directory-tree'),
fs = require('fs'),
path = require('path');
mime = require('mime-types');
var console = process.console;

var fileService = {};

fileService.get = function(req, res) {
var fileFullPath = req.params.file_id;
fs.readFile(fileFullPath, 'utf8', function(err, data) {
if (err) {
res.json(err);
console.time().tag('FILE CONTENT')
.error('file-get returned an error: ' + err);
} else {
res.json(data);
console.time().tag('FILE CONTENT')
.info('file requested: ' + fileFullPath);
}
});
var mimeType = mime.lookup(fileFullPath) || '';

var isFileOfType = function(type) {
return mimeType.indexOf(type) !== -1;
};

var showNoContent = false ||
isFileOfType('zip') ||
isFileOfType('program') ||
isFileOfType('image') ||
isFileOfType('font');

// temprorary solution until we have a view selector on the FRONT-END
if (showNoContent) {
res.json({
content: 'awww man... we can\'t show ' + mimeType + ' yet :-(',
mimeType: mimeType
});
} else {
fs.readFile(fileFullPath, 'utf8', function(err, data) {
if (err) {
res.json(err);
console.time().tag('FILE CONTENT')
.error('file-get returned an error: ' + err);
} else {
var file = {
content: data,
mimeType: mimeType
};
res.json(file);
console.time().tag('FILE CONTENT')
.info('file requested: ' + fileFullPath);
}
});
}
};

fileService.put = function(req, res) {
Expand Down
113 changes: 57 additions & 56 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,57 +1,58 @@
{
"name": "kibibit-code-editor",
"version": "0.0.0",
"description": "code editor on the cloud",
"main": "server.js",
"scripts": {
"postinstall": "node ./node_modules/bower/lib/bin/bower install && node ./node_modules/gulp/bin/gulp styles",
"start": "node server.js",
"test": "gulp test"
},
"repository": {
"type": "git",
"url": "git://github.com/Kibibit/kibibit-code-editor.git"
},
"author": "[email protected]",
"license": "BSD-2-Clause",
"engines": {
"node": "4.2.3",
"npm": "3.8.2"
},
"dependencies": {
"bcrypt-nodejs": "0.0.3",
"body-parser": "^1.14.2",
"colors": "~1.1.2",
"directory-tree": "~0.1.1",
"express": "~4.13.3",
"scribe-js": "~2.0.4",
"serve-favicon": "~2.3.0",
"user-home": "^2.0.0"
},
"devDependencies": {
"bower": "^1.7.2",
"gulp": "~3.9.0",
"gulp-cached": "^1.1.0",
"gulp-concat": "~2.6.0",
"gulp-cssnano": "^2.1.0",
"gulp-depcheck": "^1.1.0",
"gulp-develop-server": "~0.5.0",
"gulp-help": "^1.6.1",
"gulp-jsbeautifier": "~1.0.1",
"gulp-jscs": "~3.0.2",
"gulp-livereload": "~3.8.1",
"gulp-rename": "~1.2.2",
"gulp-sass": "~2.1.1",
"gulp-shell": "~0.5.1",
"gulp-sourcemaps": "~1.6.0",
"gulp-util": "~3.0.7",
"jasmine-core": "~2.4.1",
"jscs": "~2.9.0",
"jscs-angular": "~1.2.1",
"karma": "~0.13.16",
"karma-jasmine": "~0.3.6",
"karma-phantomjs-launcher": "~1.0.0",
"phantomjs-prebuilt": "^2.1.3",
"yargs": "~3.32.0"
}
}
"name": "kibibit-code-editor",
"version": "0.0.0",
"description": "code editor on the cloud",
"main": "server.js",
"scripts": {
"postinstall": "node ./node_modules/bower/lib/bin/bower install && node ./node_modules/gulp/bin/gulp styles",
"start": "node server.js",
"test": "gulp test"
},
"repository": {
"type": "git",
"url": "git://github.com/Kibibit/kibibit-code-editor.git"
},
"author": "[email protected]",
"license": "BSD-2-Clause",
"engines": {
"node": "4.2.3",
"npm": "3.8.2"
},
"dependencies": {
"bcrypt-nodejs": "0.0.3",
"body-parser": "^1.14.2",
"colors": "~1.1.2",
"directory-tree": "~0.1.1",
"express": "~4.13.3",
"mime-types": "^2.1.11",
"scribe-js": "~2.0.4",
"serve-favicon": "~2.3.0",
"user-home": "^2.0.0"
},
"devDependencies": {
"bower": "^1.7.2",
"gulp": "~3.9.0",
"gulp-cached": "^1.1.0",
"gulp-concat": "~2.6.0",
"gulp-cssnano": "^2.1.0",
"gulp-depcheck": "^1.1.0",
"gulp-develop-server": "~0.5.0",
"gulp-help": "^1.6.1",
"gulp-jsbeautifier": "~1.0.1",
"gulp-jscs": "~3.0.2",
"gulp-livereload": "~3.8.1",
"gulp-rename": "~1.2.2",
"gulp-sass": "~2.1.1",
"gulp-shell": "~0.5.1",
"gulp-sourcemaps": "~1.6.0",
"gulp-util": "~3.0.7",
"jasmine-core": "~2.4.1",
"jscs": "~2.9.0",
"jscs-angular": "~1.2.1",
"karma": "~0.13.16",
"karma-jasmine": "~0.3.6",
"karma-phantomjs-launcher": "~1.0.0",
"phantomjs-prebuilt": "^2.1.3",
"yargs": "~3.32.0"
}
}
28 changes: 21 additions & 7 deletions public/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ angular.module('kibibitCodeEditor',
'jsonFormatter',
'ngclipboard',
'ng.deviceDetector'])

.config(['$compileProvider', function($compileProvider) {
$compileProvider.debugInfoEnabled(false);
}])

.config(['ScrollBarsProvider', function(ScrollBarsProvider) {
// the following settings are defined for all scrollbars unless the
// scrollbar has local scope configuration
Expand All @@ -33,6 +35,7 @@ angular.module('kibibitCodeEditor',
autoHideScrollbar: true
};
}])

.config(['markedProvider', function(markedProvider) {
markedProvider.setOptions({
gfm: true,
Expand All @@ -50,20 +53,31 @@ angular.module('kibibitCodeEditor',
}
});
}])

.filter('marked', ['marked', function(marked) {
return function(input) {
return marked(input || '');
};
}])

.constant('CODE_EDITOR', {
'MODE_LIST': ace.require('ace/ext/modelist'),
'THEME_LIST': ace.require('ace/ext/themelist')
})
.constant('TYPE_ERROR_MSGS', function(varName, typeExpected, typeRecieved) {
return [
varName,
' should be a ',
typeExpected,
' but was given a ',
typeRecieved].join('');

.constant('ERROR_MSGS', {
'TYPE_ERROR': function(varName, typeExpected, typeRecieved) {
return [
varName,
' should be a ',
typeExpected,
' but was given a ',
typeRecieved].join('');
},
'MATCH_ERROR': function(varName, collectionName) {
return [
varName,
'value should exist in ',
collectionName].join('');
}
});
46 changes: 11 additions & 35 deletions public/app/components/codeEditor/codeEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ angular.module('kibibitCodeEditor')
// save the content of the editor on-change
vm.aceChanged = function(_editor) {
vm.aceDocumentValue = vm.aceSession.getDocument().getValue();
var fileMode = getModeFromMimeType(vm.fileInfo);
editorSettings.syntaxMode = fileMode;
console.debug('changed mode to ' + fileMode);
};

vm.attachedEditorFunctions = {
Expand All @@ -73,43 +76,16 @@ angular.module('kibibitCodeEditor')

vm.updateFileContent = function(filePath) {
if (filePath !== '') {
FileService.getFile(filePath, function(fileContent) {
vm.code = fileContent.data;
FileService.getFile(filePath, function(fileInfo) {
vm.fileInfo = fileInfo.data;
vm.code = vm.fileInfo.content;
});
}
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thatkookooguy
What about this directive?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't attached anywhere. It was a leftover from an experiment I did in a previous PR

}
])

.directive('kbChangeAceScroll', function() {
return {
scope: false,
link: function(scope, element, attrs) {
var scrollbarY = element.parent().find('.ace_scrollbar.ace_scrollbar-v');
var scrollbarX = element.parent().find('.ace_scrollbar.ace_scrollbar-h');
scope.$watch(function() {
return scrollbarY.find('.ace_scrollbar-inner').height();
}, updateY);
scope.$watch(function() {
return scrollbarX.find('.ace_scrollbar-inner').width();
}, updateX);
scope.config = {
scrollButtons: {
scrollAmount: 'auto', // scroll amount when button pressed
enable: false // enable scrolling buttons by default
},
scrollInertia: 400, // adjust however you want
axis: 'yx', // enable 2 axis scrollbars by default,
theme: 'minimal',
autoHideScrollbar: true
};
function updateY(newVal, oldVal) {
console.log('update scroll Y');
}

function updateX(newVal, oldVal) {
console.log('update scroll X');
}
function getModeFromMimeType(file) {
var getModeRegex = /\/(x-)?(.*)$/;
return file && file.mimeType ? file.mimeType.match(getModeRegex)[2] : 'text';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line must be at most 80 characters

}
};
});
}
]);
Loading