Raphael
ee556d859e
All checks were successful
Neutral/pipeline/head This commit looks good
revert Version 1.0.0 - Basic Web Application
268 lines
5.5 KiB
JavaScript
268 lines
5.5 KiB
JavaScript
const { LogType } = require("loguix")
|
|
const fs = require("fs")
|
|
const path = require("path")
|
|
const { __glob } = require("./global-variables")
|
|
const clog = new LogType("Fichier")
|
|
const os = require("os");
|
|
const uuid = require('uuid');
|
|
var mime = require('mime-types');
|
|
|
|
|
|
// check if shared folder exist, if not create it
|
|
if(!fs.existsSync(__glob.SHARED)) {
|
|
fs.mkdirSync(__glob.SHARED)
|
|
}
|
|
|
|
module.exports.getFiles = function(root) {
|
|
|
|
var response = new Object()
|
|
response.content = new Array()
|
|
|
|
|
|
try{
|
|
|
|
if(root == "homepath") {
|
|
|
|
root = os.homedir()
|
|
|
|
}
|
|
|
|
if(root == "sharepath") {
|
|
|
|
root = __glob.SHARED
|
|
|
|
}
|
|
|
|
if(root == "logpath") {
|
|
|
|
root = __glob.LOGS
|
|
|
|
}
|
|
|
|
|
|
if(!fs.existsSync(root)) {
|
|
response.content = "NOT_EXIST"
|
|
|
|
} else {
|
|
|
|
for(var file of fs.readdirSync(root)) {
|
|
|
|
const stat = fs.statSync(root + path.sep + file)
|
|
|
|
response.content.push({"name":file, id: uuid.v4().toString() ,"fileDirectory" : root + path.sep + file , "type":mime.lookup(file), "size":stat.size, "lastedition":stat.mtimeMs, "directory":stat.isDirectory()})
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}catch(err) {
|
|
|
|
response.content = "NOT_PERMITTED"
|
|
}
|
|
|
|
response.root = root
|
|
response.parent = path.dirname(root)
|
|
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
module.exports.createFolder = function(root) {
|
|
|
|
// Check if folder already exist
|
|
|
|
if(!fs.existsSync(root)) {
|
|
|
|
try {
|
|
fs.mkdirSync(root)
|
|
return "OK"
|
|
} catch(err) {
|
|
|
|
return "NOT_PERMITTED"
|
|
}
|
|
|
|
} else {
|
|
|
|
return 'EXIST'
|
|
}
|
|
|
|
}
|
|
|
|
module.exports.deleteFile = function(root) {
|
|
|
|
if(fs.existsSync(root)) {
|
|
|
|
try {
|
|
|
|
fs.rmSync(root, { recursive: true, force: true })
|
|
return "OK"
|
|
} catch(err) {
|
|
console.log(err)
|
|
return "NOT_PERMITTED"
|
|
}
|
|
} else {
|
|
|
|
return "NOT_EXIST"
|
|
}
|
|
}
|
|
|
|
module.exports.renameFile = function(settings) {
|
|
|
|
if(fs.existsSync(settings.root)) {
|
|
|
|
try {
|
|
|
|
fs.renameSync(settings.root + path.sep + settings.oldName, settings.root + path.sep + settings.newName)
|
|
return "OK"
|
|
} catch(err) {
|
|
|
|
return "NOT_PERMITTED"
|
|
}
|
|
} else {
|
|
|
|
return "NOT_EXIST"
|
|
}
|
|
}
|
|
|
|
module.exports.shareFile = function(settings) {
|
|
|
|
|
|
if(fs.existsSync(settings.root)) {
|
|
|
|
try {
|
|
//Create a sybolic link to shared folder
|
|
|
|
fs.symlinkSync(settings.root + path.sep + settings.name, __glob.SHARED + path.sep + settings.name)
|
|
|
|
// return the link of the file shared
|
|
if(process.env.DEV) {
|
|
return "http://localhost:3001/shared/" + settings.name
|
|
|
|
} else {
|
|
return "https://neutral.raphix.fr/shared/" + settings.name
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
console.log(err)
|
|
return "NOT_PERMITTED"
|
|
}
|
|
} else {
|
|
|
|
return "NOT_EXIST"
|
|
}
|
|
}
|
|
|
|
|
|
module.exports.getFile = function(root) {
|
|
|
|
if(fs.existsSync(root)) {
|
|
|
|
try {
|
|
|
|
// Check if the file is an image and if it is return the base64
|
|
if(mime.lookup(root).includes("image")) {
|
|
return "data:" + mime.lookup(root) + ";base64," + fs.readFileSync(root, "base64")
|
|
} else {
|
|
return fs.readFileSync(root, "utf-8")
|
|
}
|
|
|
|
|
|
} catch(err) {
|
|
console.log(err)
|
|
return "NOT_PERMITTED"
|
|
}
|
|
} else {
|
|
|
|
return "NOT_EXIST"
|
|
}
|
|
|
|
}
|
|
|
|
module.exports.saveFile = function(settings) {
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(settings.root + path.sep + settings.name, settings.content)
|
|
return "OK"
|
|
} catch(err) {
|
|
console.log(err)
|
|
return "NOT_PERMITTED"
|
|
}
|
|
}
|
|
|
|
module.exports.downloadFile = function(root) {
|
|
|
|
if(fs.existsSync(root)) {
|
|
|
|
try {
|
|
|
|
return fs.readFileSync(root)
|
|
} catch(err) {
|
|
console.log(err)
|
|
return "NOT_PERMITTED"
|
|
}
|
|
} else {
|
|
|
|
return "NOT_EXIST"
|
|
}
|
|
}
|
|
|
|
|
|
|
|
module.exports.uploadFile = function(settings) {
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(settings.root + path.sep + settings.name, settings.file)
|
|
return "OK"
|
|
} catch(err) {
|
|
console.log(err)
|
|
return "NOT_PERMITTED"
|
|
}
|
|
}
|
|
|
|
module.exports.newFile = function(settings) {
|
|
|
|
if(!fs.existsSync(settings)) {
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(settings, "")
|
|
return "OK"
|
|
} catch(err) {
|
|
console.log(err)
|
|
return "NOT_PERMITTED"
|
|
}
|
|
} else {
|
|
|
|
return "EXIST"
|
|
}
|
|
}
|
|
|
|
module.exports.pasteFile = function(settings) {
|
|
if(settings.action == "cut") {
|
|
|
|
try {
|
|
|
|
fs.renameSync(settings.file.fileDirectory, settings.newPath + path.sep + settings.name)
|
|
return "OK"
|
|
} catch(err) {
|
|
console.log(err)
|
|
return "NOT_PERMITTED"
|
|
}
|
|
} else if(settings.action == "copy") {
|
|
|
|
try {
|
|
fs.copyFileSync(settings.file.fileDirectory, settings.newPath + path.sep + settings.name)
|
|
return "OK"
|
|
} catch(err) {
|
|
console.log(err)
|
|
return "NOT_PERMITTED"
|
|
}
|
|
}
|
|
} |