Permalink
Browse files

abstracted out file handling

  • Loading branch information...
1 parent 422803c commit 6ce2472dfba0e8c489859ea536ebf45d2f4587ca @coolaj86 coolaj86 committed May 20, 2012
Showing with 324 additions and 202 deletions.
  1. +9 −0 README.md
  2. +14 −13 package.json
  3. +1 −1 server.js
  4. +74 −0 server/lib/file-db.js
  5. +226 −188 server/lib/index.js
View
@@ -69,6 +69,15 @@ at your Home Office, or Business please contact <coolaj86@gmail.com>.
Quick Start for Running your own DropShare
---
+If you don't want to customize it at all you can install it via npm
+
+ 0. Download [NodeJS](http://nodejs.org#download)
+ 1. `npm install -g dropshare`
+ 2. `dropshare-server`
+ 3. The database will be in `dropshare/server/lib/db/db.json`
+
+But for production use with a real database
+
0. Install [NodeJS](http://nodejs.org): [Linux Binary](https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager) | [Linux Source](http://apptob.org) | [OS X | Windows](http://nodejs.org#download).
0. Install `redis`. See Appendix (below) for installing redis on OS X.
View
@@ -25,19 +25,20 @@
"dropshare-server": "bin/dropshare-server.js"
},
"dependencies": {
- "connect": "*"
- , "connect_router": ">= 1"
- , "mime": "~1.2"
- , "fs.extra": "1.x"
- , "connect-cors": "~0.5"
- , "sequence": "~2.1"
- , "redis": "0.6.x"
- , "futures": "2.3.x"
- , "formaline": "0.6.x"
- , "numeric-buffer": "0.1.x"
- , "dom-storage": "1.x"
- , "json-storage": "1.x"
- , "mkdirp": "0.3.x"
+ "connect": "*",
+ "connect_router": ">= 1",
+ "mime": "~1.2",
+ "fs.extra": "1.x",
+ "connect-cors": "~0.5",
+ "sequence": "~2.1",
+ "redis": "0.6.x",
+ "futures": "2.3.x",
+ "formaline": "0.6.x",
+ "numeric-buffer": "0.1.x",
+ "dom-storage": "1.x",
+ "json-storage": "1.x",
+ "mkdirp": "0.3.x",
+ "forEachAsync": "~2.1.1"
},
"devDependencies": {}
}
View
@@ -8,7 +8,7 @@
"tmp": "/tmp"
, "storageDir": __dirname + "/files"
, "client": __dirname + "/public"
- //, "databaseStrategy": "json"
+ , "databaseStrategy": "json"
}
, app
, attributeName
View
@@ -0,0 +1,74 @@
+/*jshint laxcomma:true es5:true node:true onevar:true*/
+(function () {
+ "use strict";
+
+ var fs = require('fs.extra')
+ , path = require('path')
+ , assert = require('assert')
+ ;
+
+ function FileDb(privateDir) {
+ var self = this
+ ;
+
+ self._privateDir = privateDir;
+ }
+ FileDb.prototype.exists = function (cb, key) {
+ var self = this
+ ;
+
+ // path.exists
+ fs.stat(path.join(self._privateDir, key), function (e, data) {
+ if (!e || !data) {
+ cb(false);
+ return;
+ }
+
+ cb(true);
+ });
+ };
+
+ FileDb.prototype.put = function (cb, fileStat) {
+ var self = this
+ , newFilePath = self._privateDir + '/' + fileStat.sha1checksum
+ ;
+
+ fs.stat(fileStat.path, function (err, stat) {
+ assert.strictEqual(err, null, "tried to move a non-existent file");
+
+ //check if file with same checksum already exists
+ fs.stat(newFilePath, function (statErr, stat) {
+ if (typeof stat === "undefined") {
+ // File does not exist already, so move it in to place
+ fs.move(fileStat.path, newFilePath, function (movErr) {
+ if (movErr) {
+ console.warn('Error moving file to storage: ' + movErr.toString());
+ //throw err;
+ }
+ });
+ }
+ else {
+ // TODO check that they aren't each other
+ fs.unlink(fileStat.path);
+ }
+
+ cb(null, fileStat.sha1checksum, fileStat);
+ });
+ });
+ };
+
+ FileDb.prototype.remove = function (cb, fileStoreKey) {
+ fs.unlink(path.join(this._privateDir, fileStoreKey), cb);
+ };
+ FileDb.prototype.link = function (cb, fileStoreKey, toHere) {
+ var self = this
+ ;
+
+ fs.link(path.join(self._privateDir, fileStoreKey), toHere, cb);
+ };
+ FileDb.create = function (a, b, c) {
+ return new FileDb(a, b, c);
+ };
+
+ module.exports = FileDb;
+}());
Oops, something went wrong.

0 comments on commit 6ce2472

Please sign in to comment.