MongoDB GridFS-是文件名还是文件名 [英] MongoDB GridFS - Is it filename or fileName

查看:132
本文介绍了MongoDB GridFS-是文件名还是文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看以下图片,来自 http://mongoexplorer.com/:

Please look at the following image, from http://mongoexplorer.com/:

我一直在尝试通过GridFS,引用 https://github.com/jamescarr/nodejs-mongodb-streaming .我上传的文件可以很好地返回,并且通过以下get函数返回的流看起来很正确.

I've been trying to work through GridFS, referencing https://github.com/jamescarr/nodejs-mongodb-streaming. The files I uploaded, come back nicely and the stream that comes back via the following get function looks right.

var gridfs = (function () {
    function gridfs() { }
    gridfs.get = function (id, fn) {
        var db, store;
        db = mongoose.connection.db;
        id = new ObjectID(id);
        store = new GridStore(db, id, "r", {
            root: "fs"
        });
        return store.open(function (err, store) {
            if (err) {
                return fn(err);
            }
            return fn(null, store);
        });
    };
    return gridfs;
})();

使用 http://mongoexplorer.com/我将文件上传到GridFS进行测试,但看起来好像坏了当我使用上面的节点代码来检索它们时.

Using http://mongoexplorer.com/ I uploaded files into GridFS to test with, but they seem broken when I use the node code above to retrieve them.

那是我注意到filename/fileName的东西.在这里/node_modules/mongodb/lib/mongodb/gridfs/gridstore.js ,我看到对文件名的引用使用小写的"N",但是在我的GridFS中,它是具有大写字母"N"的fileName.

That is when I noticed the filename / fileName thing. Looking here /node_modules/mongodb/lib/mongodb/gridfs/gridstore.js I saw the reference to filename with a lowercase 'N', but in my GridFS, it's fileName with a capital 'N'.

好的,所以只是为了踢一下,我在GridFS中将其更改为小写,但是在检索使用 http://mongoexplorer.com/中单击另存为... ,但是会带来完美地退还我的罚款.

OK, so just for kicks, I changed it to lowercase in GridFS, but I still get some corruption in the stream (node code above) when retrieving files uploaded with http://mongoexplorer.com/. Clicking Save as... in http://mongoexplorer.com/, however brings back my fine just perfectly.

回到我的问题,(因为我的测试似乎没有证明什么,)我想知道是哪一个:小写字母"N"的文件名或大写字母"N"的文件名?

To get back to my question, (since my tests didn't seem to prove anything,) I am wondering which is it: filename with a lowercase 'N', or fileName with 'N' in caps?

推荐答案

另一个Windows工具nl. MongoVue还会查找 filename 而不是 fileName .我会说答案更可能是文件名而不是fileName.

Another Windows tool nl. MongoVue also looks for filename instead of fileName. I'd say the answer is more likely filename instead of fileName.

从GridStore检索小的Windows文件时,我发现了一个错误,但是我不知道如何解决它.我猜必须有一些类似Chunk.CurrentSize之类的值,但是要查看本机节点mongo驱动程序

With retrieving the small Windows file from GridStore, I found a bug, but I don't know how to fix it. I guess there must be some value like Chunk.CurrentSize or the like, but looking at the chunk.js file in the native node mongo driver https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/gridfs/chunk.js, I did the following...

我发现了:

Chunk.prototype.readSlice = function(length) {
  if ((this.length() - this.internalPosition + 1) >= length) {
    var data = null;
    if (this.data.buffer != null) { //Pure BSON
      data = this.data.buffer.slice(this.internalPosition, this.internalPosition + length);
    } else { //Native BSON
      data = new Buffer(length);
      length = this.data.readInto(data, this.internalPosition);
    }
    this.internalPosition = this.internalPosition + length;
    return data;
  } else {
    return null;
  }
};

并移动了以下

data = this.data.buffer.slice(this.internalPosition, this.internalPosition + length);

进入this if语句(1024 * 256是Chunk.DEFAULT_CHUNK_SIZE = 1024 * 256;中的值)

into the this if statement (1024 * 256 is the value from Chunk.DEFAULT_CHUNK_SIZE = 1024 * 256;)

    if (this.data.buffer != null) { //Pure BSON
      if (this.data.buffer.length > 1024 * 256) {
        // move to here
      } 
      else 
      {
        data = this.data.buffer;
      }

像这样:

Chunk.prototype.readSlice = function(length) {
  if ((this.length() - this.internalPosition + 1) >= length) {
    var data = null;
    if (this.data.buffer != null) { //Pure BSON
      if (this.data.buffer.length > 1024 * 256) {
        data = this.data.buffer.slice(this.internalPosition, this.internalPosition + length);
      } 
      else 
      {
        data = this.data.buffer;
      }
    } else { //Native BSON
      data = new Buffer(length);
      length = this.data.readInto(data, this.internalPosition);
    }
    this.internalPosition = this.internalPosition + length;
    return data;
  } else {
    return null;
  }
};

解决了Windows文件小于块大小的问题,但这不是最优雅的解决方案.我想以此为答案,但是我意识到,使用默认的硬编码的块大小不是动态值,这会减少这种变通方法;-)

The issue with windows files smaller than the chunk size is solved, but this isn't the most elegant solution. I'd like to propose this as the answer, but I realize using the default chunk size hard coded isn't the dynamic value which would make this less of a workaround ;-)

这篇关于MongoDB GridFS-是文件名还是文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆