Skip to content

Commit

Permalink
Update to browse.
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamRClark committed Jun 1, 2021
1 parent 295a81e commit 1dabe42
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 121 deletions.
162 changes: 42 additions & 120 deletions api/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,17 @@

import express, { response } from 'express';
const cors = require('cors');

const HOME_DIR = require('os').homedir();
var fs = require("fs");
const fsPromises = fs.promises;
var path = require("path");

export const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());


const BASE_URI= '/api';
const HOME_DIR = require('os').homedir();
var runtimeAPI;
var fs = require("fs");
var path = require("path");


app.get('*', function (req, res) {
Auth.signUp({
username: 'AmandaB',
password: 'MyCoolPassword1!',
attributes: {
email: 'someemail@example.com',
},
});

res.status(500);
res.send('API Response to route:' + req.originalUrl);
});

async function getStats(dirPath) {
async function getStats(dirPath: any) {
var stats = await fsPromises.stat(dirPath);
return {
folder: stats.isDirectory(),
Expand All @@ -52,108 +34,48 @@ async function getStats(dirPath) {
}
}

module.exports = {
init: function(_runtimeAPI) {
runtimeAPI = _runtimeAPI
},

sheets: function (req, res) {
let pathParam = req.params.path;
// Path needs to be url encoded.
if (pathParam) {
pathParam = decodeURIComponent(pathParam);
}

// Read sheet names from Excel file.
const workbook = XLSX.readFile(pathParam, { bookSheets: true });

// Typical workbook response will look like: {"SheetNames":["Simple Tax Forecast"]}
res.json(workbook);
},

sheet: function (req, res) {
let pathParam = req.params.path;
let sheetParam = req.params.sheetId;
// Path needs to be url encoded.
if (pathParam) {
pathParam = decodeURIComponent(pathParam);
}

if (sheetParam) {
sheetParam = decodeURIComponent(sheetParam);
}

// Read sheet names from Excel file.
const workbook = XLSX.readFile(pathParam);
const worksheet = workbook.Sheets[sheetParam];
if (!worksheet) {
node.status({ fill: "red", shape: "circle", text: "Sheet does not exist: " + config.sheetname });
node.error("Sheet does not exist: " + config.sheetname, msg.payload.msg);
return;
}
let sheetHtml = XLSX.utils.sheet_to_html(worksheet, { header: '', footer: ''});
res.send(sheetHtml);
},

browse: function (req, res) {
let pathParam = req.params.path;
// Path needs to be url encoded.
if (pathParam) {
pathParam = decodeURIComponent(pathParam);
}

dialog.fileselect("Select message", "Select title", 3000, function(code, retVal, stderr) {
if (retVal) {
let returnPath = retVal.replace(/(\r\n|\n|\r)/gm, ""); // Get rid of trailing carriage returns.
res.json({path: returnPath});
} else {
apiUtils.rejectHandler(req, res, stderr);
}
});
},

root: async function(req, res) {
app.get('/encanto/root', async function(req, res) {
res.json({"path": HOME_DIR})
},

list: async function(req, res) {
let dirPath = req.params.path;
// Path needs to be url encoded.
if (dirPath) {
dirPath = decodeURIComponent(dirPath);
} else {
dirPath = '';
}
dirPath = path.join(HOME_DIR, dirPath);

var stats = [];
try {
var files = await fsPromises.readdir(dirPath);
try {
for (var i=0; i<files.length; ++i) {
var fPath = path.join(dirPath, files[i]);
var stat = await getStats(fPath);
stat.name = files[i];
});

// We should filter everything except for folders and spreadsheets here.
if (stat.folder === true ||
stat.folder === false && fPath.endsWith('.xlsx')) {
app.get('/encanto/list/:path?', async function(req, res) {
let dirPath = req.params.path;
// Path needs to be url encoded.
if (dirPath) {
dirPath = decodeURIComponent(dirPath);
} else {
dirPath = '';
}
dirPath = path.join(HOME_DIR, dirPath);

var stats = [];
try {
var files = await fsPromises.readdir(dirPath);
try {
for (var i=0; i<files.length; ++i) {
var fPath = path.join(dirPath, files[i]);
var stat: any = await getStats(fPath);
stat.name = files[i];

// Example of filtering of a file type.
/*
if (stat.folder === true ||
stat.folder === false && fPath.endsWith('.xlsx')) {
stats.push(stat);
}
}
} catch (e) {
log.info('Error reading file in directory: ' + dirPath);
stats = [];
}
} catch (e) {
log.info('Error reading directory: ' + dirPath);
stats = [];
}
*/
stats.push(stat);
}
res.send(stats);
} catch (e) {
console.error('Error reading file in directory: ' + dirPath);
stats = [];
}
} catch (e) {
console.error('Error reading directory: ' + dirPath);
stats = [];
}

}

res.send(stats);
});

console.log('Start without ssl');

Expand Down
2 changes: 1 addition & 1 deletion component/src/app/file-browse/file-browse.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class FileBrowseComponent implements OnInit {
public doSelectFile() {
// Compute the full path.
if (this.selectedFile) {
console.log('file selected: ' + this.rootPath + this.getCurrentPath() + '/' + this.selectedFile.name);
alert('file selected: ' + this.rootPath + this.getCurrentPath() + '/' + this.selectedFile.name);
// Dispatch using window custom event so we can listen from anywhere.
let event = new CustomEvent("fselect", {
detail: {
Expand Down

0 comments on commit 1dabe42

Please sign in to comment.