Skip to content

Commit

Permalink
[Fix] Can not access specfic filename with \#?..
Browse files Browse the repository at this point in the history
  • Loading branch information
efeiefei committed Apr 21, 2017
1 parent 8005160 commit c5c8b0b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/fileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ FileManager.rename = function *(src, dest) {
yield fse.move(src, dest);
};

FileManager.archive = function *(dirPath, archive, src, embedDirs) {
FileManager.archive = function *(src, archive, dirPath, embedDirs) {
var zip = new JSZip();
var baseName = path.basename(archive, '.zip');

Expand Down Expand Up @@ -83,7 +83,7 @@ FileManager.archive = function *(dirPath, archive, src, embedDirs) {

// Add each src. For directories, do the entire recursive dir.
for (var file of src) {
yield * process(path.join(dirPath, file.replace(/^\//, '')));
yield * process(file);
}

// Generate the zip and store the final.
Expand Down
5 changes: 3 additions & 2 deletions lib/fileMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ var path = require('path');

var DATA_ROOT = C.data.root;

exports.filePath = function (relPath) {
exports.filePath = function (relPath, decodeURI) {
if (decodeURI) relPath = decodeURIComponent(relPath);
if (relPath.indexOf('..') >= 0){
var e = new Error('Do Not Contain .. in relPath!');
e.status = 400;
Expand All @@ -11,4 +12,4 @@ exports.filePath = function (relPath) {
else {
return path.join(DATA_ROOT, relPath);
}
};
};
14 changes: 8 additions & 6 deletions lib/public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ FMApp.controller('FileManagerCtr', ['$scope', '$http', '$location',
.success(function (data) {
var files = data;
files.forEach(function (file) {
file.relPath = relPath + file.name;
file.relPath = relPath + encodeURIComponent(file.name);
if (file.folder) file.relPath += '/';
file.selected = false;
file.humanSize = humanSize(file.size);
file.humanTime = humanTime(file.mtime);
});
FM.curFiles = files;
console.log('Current Files:');
console.log(FM.curFiles);
})
.error(function (data, status) {
alert('Error: ' + status + data);
Expand Down Expand Up @@ -175,7 +177,7 @@ FMApp.controller('FileManagerCtr', ['$scope', '$http', '$location',
};

FM.move = function (target) {
var url = 'api' + target;
var url = 'api' + encodeURI(target);
var src = FM.selection.map(function (file) {
return file.relPath;
});
Expand All @@ -186,7 +188,7 @@ FMApp.controller('FileManagerCtr', ['$scope', '$http', '$location',
if (!archive.match(/\.zip$/)) {
archive += '.zip';
}
var url = 'api' + FM.curFolderPath + archive;
var url = 'api' + FM.curFolderPath + encodeURI(archive);
var src = FM.selection.map(function (file) {
return file.relPath;
});
Expand All @@ -195,21 +197,21 @@ FMApp.controller('FileManagerCtr', ['$scope', '$http', '$location',

FM.rename = function (newName) {
var url = 'api' + FM.selection[0].relPath;
var target = FM.curFolderPath + newName;
var target = FM.curFolderPath + encodeURI(newName);
console.log('rename target', target);
httpRequest('PUT', url, {type: 'RENAME'}, {target: target});
};

FM.createFolder = function (folderName) {
var url = 'api' + FM.curFolderPath + folderName;
var url = 'api' + FM.curFolderPath + encodeURI(folderName);
httpRequest('POST', url, {type: 'CREATE_FOLDER'}, null);
};

FM.upload = function () {
console.log('Upload File:', FM.uploadFile);
var formData = new FormData();
formData.append('upload', FM.uploadFile);
var url = 'api' + FM.curFolderPath + FM.uploadFile.name;
var url = 'api' + FM.curFolderPath + encodeURI(FM.uploadFile.name);
httpRequest('POST', url, {type: 'UPLOAD_FILE'}, formData, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
Expand Down
10 changes: 7 additions & 3 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ router.put('/api/(.*)', Tools.loadRealPath, Tools.checkPathExists, bodyParser(),
var src = this.request.body.src;
if (!src || ! (src instanceof Array)) return this.status = 400;
var src = src.map(function (relPath) {
return FilePath(relPath);
return FilePath(relPath, true);
});
yield * FileManager.move(src, p);
this.body = 'Move Succeed!';
}
else if (type === 'RENAME') {
var target = this.request.body.target;
if (!target) return this.status = 400;
yield * FileManager.rename(p, FilePath(target));
yield * FileManager.rename(p, FilePath(target, true));
this.body = 'Rename Succeed!';
}
else {
Expand Down Expand Up @@ -93,7 +93,11 @@ router.post('/api/(.*)', Tools.loadRealPath, Tools.checkPathNotExists, bodyParse
else if (type === 'CREATE_ARCHIVE') {
var src = this.request.body.src;
if (!src) return this.status = 400;
yield * FileManager.archive(C.data.root, p, src, !!this.request.body.embedDirs);
src = src.map(function(file) {
return FilePath(file, true);
})
var archive = p;
yield * FileManager.archive(src, archive, C.data.root, !!this.request.body.embedDirs);
this.body = 'Create Archive Succeed!';
}
else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-file-manager",
"version": "0.4.0",
"version": "0.4.5",
"description": "File manager web based on Koa and Angular.js",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit c5c8b0b

Please sign in to comment.