From 4d48c88807b49a88afc73222b9d47415852951c3 Mon Sep 17 00:00:00 2001 From: ansuz Date: Fri, 28 Apr 2017 12:49:08 +0200 Subject: [PATCH] create blob and blobstage stores if not exists --- rpc.js | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/rpc.js b/rpc.js index 5670f1c7a..4d1e0005d 100644 --- a/rpc.js +++ b/rpc.js @@ -2,6 +2,8 @@ /* Use Nacl for checking signatures of messages */ var Nacl = require("tweetnacl"); +var Fs = require("fs"); + var RPC = module.exports; var Store = require("./storage/file"); @@ -380,19 +382,17 @@ var getLimit = function (cb) { }; -var createBlobStaging = function (cb) { - -}; - -var createBlobStore = function (cb) { +var safeMkdir = function (path, cb) { + Fs.mkdir(path, function (e) { + if (!e || e.code === 'EEXIST') { return void cb(); } + cb(e); + }); }; var upload = function (store, Sessions, publicKey, cb) { }; - - /*::const ConfigType = require('./config.example.js');*/ RPC.create = function (config /*:typeof(ConfigType)*/, cb /*:(?Error, ?Function)=>void*/) { // load pin-store... @@ -509,15 +509,29 @@ RPC.create = function (config /*:typeof(ConfigType)*/, cb /*:(?Error, ?Function) } }; + var keyOrDefaultString = function (key, def) { + return typeof(config[key]) === 'string'? config[key]: def; + }; + + var pinPath = keyOrDefaultString('pinPath', './pins'); + var blobPath = keyOrDefaultString('blobPath', './blob'); + var blobStagingPath = keyOrDefaultString('blobStagingPath', './blobstage'); + Store.create({ - filePath: './pins' + filePath: pinPath, }, function (s) { store = s; - cb(void 0, rpc); - // expire old sessions once per minute - setInterval(function () { - expireSessions(Sessions); - }, 60000); + safeMkdir(blobPath, function (e) { + if (e) { throw e; } + safeMkdir(blobStagingPath, function (e) { + if (e) { throw e; } + cb(void 0, rpc); + // expire old sessions once per minute + setInterval(function () { + expireSessions(Sessions); + }, 60000); + }) + }); }); };