是否有 Nodejs 模块或示例说明如何使用 WebDAV 访问 GridFS 文件?

发布于 2024-12-16 23:56:58 字数 342 浏览 0 评论 0原文

我有一个现有的 node.js 应用程序,其中用户拥有一个使用 GridFS 存储的文件库。每个用户都有自己的图书馆。我想让该库可以通过 WebDAV 安装,以便用户可以从桌面管理他们的库。

我已经看到 jsDAV 用于访问文件系统,但不清楚如何扩展它以与虚拟系统一起使用文件系统。我找到了 gitDav 但不清楚如何使用它。

这甚至可能不需要从头开始吗?

I have an existing node.js app where users have a library of files that are stored with GridFS. Each user has their own library. I would like to make the library mountable with WebDAV so that a user could manage their library from their desktop.

I have seen jsDAV used to access the filesystem but it is not clear how to extend it for use with a virtual file system. I found gitDav but it is not clear how to use it.

Is this even possible without starting from scratch?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悍妇囚夫 2024-12-23 23:56:58

我希望使用 jsDAV 通过 WebDAV 提供一些资源。由于找不到可行的示例,我研究了源代码中的注释并自己编写了一个。 jsDAV 是 PHP 库的移植。一般来说,Sabre 手册是有用的指南。要记住的一件事是,由于我们处于异步环境中,因此在 PHP 中返回结果的函数可能必须调用回调函数。当相关操作涉及从磁盘读取时,通常会发生这种情况。回调的第一个参数始终是一个错误对象,当一切顺利时,该对象应该为 null。

'use strict';

var crypto = require('crypto');

var jsDAV = require("jsDAV/lib/jsdav");
var jsDAVLocksBackendFS = require("jsDAV/lib/DAV/plugins/locks/fs");
var jsDAVFile = require("jsDAV/lib/DAV/file");
var jsDAVCollection = require("jsDAV/lib/DAV/collection");
var jsExceptions = require("jsDAV/lib/shared/exceptions");

var VirtualFile = jsDAVFile.extend(
{    
    initialize: function(name, buffer) {
        this.name = name;
        this.buffer = buffer;
    },

    getName: function() {
        return this.name;
    },

    get: function(callback) {
        callback(null, this.buffer);
    },

    put: function(data, type, callback) {
        callback(new jsExceptions.Forbidden("Permission denied to change data"));
    },

    getSize: function(callback) {
        callback(null, this.buffer.length);
    },

    getETag: function(callback) {
        var shasum = crypto.createHash('sha1');
        shasum.update(this.buffer);
        var etag = '"' + shasum.digest('hex') + '"';
        callback(null, etag);
    },

    getContentType: function(callback) {
        callback(null, 'text/plain');
    }    
});

var VirtualDirectory = jsDAVCollection.extend(
{
    initialize: function(name, children) {
        this.name = name;
        this.children = children;        
    },

    getChildren: function(callback) {
        var list = [];
        for (var name in this.children) {
            list.push(this.children[name]);
        }
        callback(null, list);
    },

    getChild: function(name, callback) {
        var child = this.children[name];
        if (child) {
            callback(null, child);
        } else {
            callback(new jsExceptions.NotFound("File not found"));
        }
    },

    childExists: function(name, callback) {
        var exists = (this.children[name] !== undefined);
        callback(null, exists);
    },

    getName: function() {
        return this.name;
    }
});

var children = {};
for (var i = 1; i <= 10; i++) {
    var name = 'file' + i + '.txt';
    var text = 'Hello world, #' + i;
    children[name] = VirtualFile.new(name, new Buffer(text, 'utf8'));
}

var grandchildren = {};
for (var i = 66; i <= 99; i++) {
    var name = 'beer' + i + '.txt';
    var text = i + ' bottles of beer';
    grandchildren[name] = VirtualFile.new(name, new Buffer(text, 'utf8'));
}
children['folder'] = VirtualDirectory.new('folder', grandchildren);

var root = VirtualDirectory.new(null, children);

var options = {
    node: root,
    locksBackend: jsDAVLocksBackendFS.new(__dirname + "/data")
};
var port = 8000;

jsDAV.createServer(options, port);

I was looking to use jsDAV to make some resources available through WebDAV. Failing to find a working example, I studied the comments in the source and wrote one myself. jsDAV is a port from a PHP library. The Sabre manual is useful guide in general. One thing to remember is that since we're in an asynchronous environment, functions that return the results in PHP might have to invoke a callback function instead. This usually happens when the operation in question involves reading from the disk. The first parameter to the callback will always be an error object, which should be null when all goes well.

'use strict';

var crypto = require('crypto');

var jsDAV = require("jsDAV/lib/jsdav");
var jsDAVLocksBackendFS = require("jsDAV/lib/DAV/plugins/locks/fs");
var jsDAVFile = require("jsDAV/lib/DAV/file");
var jsDAVCollection = require("jsDAV/lib/DAV/collection");
var jsExceptions = require("jsDAV/lib/shared/exceptions");

var VirtualFile = jsDAVFile.extend(
{    
    initialize: function(name, buffer) {
        this.name = name;
        this.buffer = buffer;
    },

    getName: function() {
        return this.name;
    },

    get: function(callback) {
        callback(null, this.buffer);
    },

    put: function(data, type, callback) {
        callback(new jsExceptions.Forbidden("Permission denied to change data"));
    },

    getSize: function(callback) {
        callback(null, this.buffer.length);
    },

    getETag: function(callback) {
        var shasum = crypto.createHash('sha1');
        shasum.update(this.buffer);
        var etag = '"' + shasum.digest('hex') + '"';
        callback(null, etag);
    },

    getContentType: function(callback) {
        callback(null, 'text/plain');
    }    
});

var VirtualDirectory = jsDAVCollection.extend(
{
    initialize: function(name, children) {
        this.name = name;
        this.children = children;        
    },

    getChildren: function(callback) {
        var list = [];
        for (var name in this.children) {
            list.push(this.children[name]);
        }
        callback(null, list);
    },

    getChild: function(name, callback) {
        var child = this.children[name];
        if (child) {
            callback(null, child);
        } else {
            callback(new jsExceptions.NotFound("File not found"));
        }
    },

    childExists: function(name, callback) {
        var exists = (this.children[name] !== undefined);
        callback(null, exists);
    },

    getName: function() {
        return this.name;
    }
});

var children = {};
for (var i = 1; i <= 10; i++) {
    var name = 'file' + i + '.txt';
    var text = 'Hello world, #' + i;
    children[name] = VirtualFile.new(name, new Buffer(text, 'utf8'));
}

var grandchildren = {};
for (var i = 66; i <= 99; i++) {
    var name = 'beer' + i + '.txt';
    var text = i + ' bottles of beer';
    grandchildren[name] = VirtualFile.new(name, new Buffer(text, 'utf8'));
}
children['folder'] = VirtualDirectory.new('folder', grandchildren);

var root = VirtualDirectory.new(null, children);

var options = {
    node: root,
    locksBackend: jsDAVLocksBackendFS.new(__dirname + "/data")
};
var port = 8000;

jsDAV.createServer(options, port);
情独悲 2024-12-23 23:56:58

看起来 jsDAV 是唯一的选择。它是 PHP 库的一个端口,它的设置方式并不能让您像普通的 Node.js 模块一样使用它。我找到了其他人创建的一些服务器类型示例,用于将其与 dropbox 和 <一个href="http://stephane.shimaore.net/git/?p=jsDAV-couchdb.git;a=commitdiff;h=6144ca16d763803fab542f2a3ddaf69965dcbe5e" rel="nofollow">couchdb。

我现在正在开发一种服务器类型,它的工作方式更像您期望的 Node.js 模块的工作方式。下一步将使其与 npm 配合良好。您可以在此处查看我的分叉。

It looks like jsDAV is the only option. It is a port of a PHP library and it is not setup in such a way that you can use it like a normal node.js module. I found a few examples of server types that others have created to connect it with dropbox and couchdb.

I am now working on a server type that will work more like you would expect a node.js module to work. The next step will be making it play nice with npm. You can see my fork here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文