Skip to content

Instantly share code, notes, and snippets.

@piboistudios
Forked from retrohacker/config.json
Created June 6, 2024 03:44
Show Gist options
  • Save piboistudios/195a22de685d3827c83f44ddc17a104b to your computer and use it in GitHub Desktop.
Save piboistudios/195a22de685d3827c83f44ddc17a104b to your computer and use it in GitHub Desktop.
storj-fuse
{
"privateKey": "[PRIVATE_KEY]",
"publicKey": "[PUBLIC_KEY]",
"encryptionKey": "[ENCRYPTION_KEY]"
}
var fuse = require('fuse-bindings');
var Storj = require('storj');
var config = require('./config.json');
var key = config.privateKey;
var encryptionKey = config.encryptionKey;
var path = require('path');
var mkdirp = require('mkdirp');
var mountPath = path.join(__dirname, 'storjfs');
mkdirp.sync(mountPath);
// Keep track of if we have unmounted the fs so we know whether or not to alert
// the user of a hanging fuse mount on exit
var mounted = false;
var storj = new Storj({ key, encryptionKey });
fuse.mount(mountPath, {
readdir: function(p, cb) {
console.log(`readdir(${p})`);
var element = path.parse(p);
var dirs = element.dir.split(path.sep).filter((v) => v !== '');
var file = element.base;
if(dirs.length !== 0) {
// Performing readdir on a file or nonsensical path
return cb(-20); // ENOTDIR: not a directory
}
return storj.getBucketList(function(e, buckets) {
if(e) {
console.error(e);
return cb(-5); // IO error
}
if(file === '') {
console.log(` * Requesting ${mountPath}`)
return cb(0, buckets.map((v) => v.name));
}
console.log(` * Requesting ${mountPath}/${file}`);
var bucket = buckets.filter((v) => v.name === file)[0];
if(!bucket) {
// We didn't find a file by that name
return cb(-2); // ENOENT: no such file
}
storj.getFileList(bucket.id, function(e, files) {
if(e) {
console.error(e);
return cb(-5); // IO error
}
console.log(files);
return cb(0, files.map((v) => v.filename));
});
});
},
getattr: function(p, cb) {
console.log(`getattr(${p})`);
var element = path.parse(p);
var dirs = element.dir.split(path.sep).filter((v) => v !== '');
var file = element.base;
if(dirs.length === 0 && file === '') {
console.log(` * Requesting ${mountPath}`);
return cb(0, {
mtime: Date.now(),
atime: Date.now(),
ctime: Date.now(),
size: 0,
mode: 16877,
uid: process.getuid ? process.getuid() : 0,
gid: process.getgid ? process.getgid() : 0,
});
} else if(dirs.length === 0) {
console.log(` * Requesting buckets`);
return storj.getBucketList(function(e, buckets) {
if(e) {
console.error(e);
return cb(-5); // IO error
}
// Get the file's metadata
var meta = buckets.filter((v) => v.name === file)[0]
if(!meta) {
// We didn't find a file by that name
return cb(-2); // ENOENT: no such file
}
return cb(null, {
mtime: (new Date(meta.created)).getTime(),
atime: Date.now(),
ctime: Date.now(),
size: 0,
mode: 16877,
uid: process.getuid ? process.getuid() : 0,
gid: process.getgid ? process.getgid() : 0,
});
});
} else if(dirs.length === 1) {
console.log(` * Requesting ${dirs[0]}/${file}`)
return storj.getBucketList(function(e, buckets) {
if(e) {
console.error(e);
return cb(-5); // IO error
}
var bucket = buckets.filter((v) => v.name === dirs[0])[0];
if(!bucket) {
// We didn't find a file by that name
return cb(-2); // ENOENT: no such file
}
storj.getFileList(bucket.id, function(e, files) {
if(e) {
console.error(e);
return cb(-5); // IO error
}
console.log(files);
var meta = files.filter((v) => v.filename === file)[0];
if(!meta) {
// We didn't find a file by that name
return cb(-2); // ENOENT: no such file
}
return cb(null, {
mtime: Date.now(),
atime: Date.now(),
ctime: Date.now(),
size: meta.size,
mode: 16877,
uid: process.getuid ? process.getuid() : 0,
gid: process.getgid ? process.getgid() : 0,
});
});
});
}
// Non sensical path
return cb(-6); // No such device or address
}
}, function (e) {
if(e) { throw e; }
console.log(`Filesystem mounted on ${mountPath}`);
mounted = true;
})
process.on('SIGINT', function() {
fuse.unmount(mountPath, function(e) {
if(e) {
// Let user retry SIGINT or choose to SIGKILL
console.error(e);
return console.error('WARNING: Failed to unmount');
}
console.log('Unmounted fs');
mounted = false;
process.exit(0);
});
});
process.on('exit', function() {
if(mounted) {
console.error('WARNING: storjfs not unmounted!');
}
});
{
"dependencies": {
"fuse-bindings": "^2.11.0",
"mkdirp": "^0.5.1",
"storj": "github:storj/storj.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment