From 833cd79524b0f8f2632ad837eec5194b26dcacb8 Mon Sep 17 00:00:00 2001 From: Iain MacDonald Date: Wed, 15 Mar 2017 17:06:33 +0800 Subject: [PATCH] Implement file scheme --- src/scheme/file.coffee | 85 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/scheme/file.coffee diff --git a/src/scheme/file.coffee b/src/scheme/file.coffee new file mode 100644 index 0000000..3a16e64 --- /dev/null +++ b/src/scheme/file.coffee @@ -0,0 +1,85 @@ +fs = require "fs" +path = require "path" +Signal = require "signals" + +module.exports = class File + + + connection : null + connected : null + failed : null + closed : null + + constructor: -> + @connected = new Signal() + @failed = new Signal() + @closed = new Signal() + + ### + Connect to the File + + @param config Configuration file for your connection + ### + connect: (config) -> + @basePath = config.basePath + @connected.dispatch() + + ### + Close the connection + ### + close: (callback) -> + @closed.dispatch() + + ### + Dispose + ### + dispose: -> + + ### + Retrieve a file on the server + + @param path: The path of your file + @param callback: Callback method + ### + get: (filePath, callback) -> + fs.readFile (path.join @basePath, filePath), 'utf8', callback + + ### + Upload a file to the server + + @param local_path: The local path of your file + @param remote_path: The remote path where you want your file to be uploaded at + @param callback: Callback method + ### + upload: (local_path, remote_path, callback) -> + targetPath = path.join @basePath, remote_path + targetDir = path.dirname targetPath + mkdirRecursive targetDir if not fs.existsSync targetDir + reader = fs.createReadStream local_path + reader.on "end", callback + reader.pipe fs.createWriteStream targetPath + + ### + Delete a file from the server + + @param remote_path: The remote path you want to delete + @param callback: Callback method + ### + delete: (remote_path, callback) -> + fs.unlink (path.join @basePath, remote_path), callback + + ### + Create a directory + + @param path: The path of the directory you want to create + @param callback: Callback method + ### + mkdir: (remote_path, callback) -> + mkdirRecursive path.join @basePath, remote_path + callback() + +mkdirRecursive = (targetDir) -> + (targetDir.split path.sep).forEach (dir, index, splits) -> + parent = (splits.slice 0, index).join path.sep + dirPath = path.resolve parent, dir + fs.mkdirSync dirPath if not fs.existsSync dirPath