<commit_before>/**
 * Created by vedi on 11/21/13.
 */

'use strict';

const Bb = require('bluebird');
const mongoose = require('mongoose');

const GridStore = Bb.promisifyAll(mongoose.mongo.GridStore);
const ObjectID = mongoose.mongo.ObjectID;

class GridFsStorage {

  initialize(options) {
    this.db = options.dataSource.ModelClass.db.db;
  }

  getStream(fileMeta) {
    const id = fileMeta.fileId;
    const store = new GridStore(this.db, new ObjectID(id.toString()), 'r', { root: 'fs' });
    Bb.promisifyAll(store);
    return store.openAsync().then(db => ({
      contentType: fileMeta.contentType,
      contentLength: db.stream(true).totalBytesToRead,
      stream: db.stream(true),
    }));
  }

  putFile(path, options) {
    const gridStore = Bb.promisifyAll(new GridStore(this.db, new ObjectID(), 'w', options));

    return Bb
      .try(() => gridStore.openAsync())
      .then(() => gridStore.writeFileAsync(path))
      .then(doc => ({
        contentType: doc.contentType,
        fileName: doc.filename,
        fileId: doc.fileId,
        root: doc.root,
        uploadDate: doc.uploadDate,
      }));
  }

  replaceFile(fileMeta, path, options) {
    const id = fileMeta.fileId;

    return Bb
      .bind(this)
      .then(() => {
        options = options || {};
        options.root = 'fs';
        const store = Bb.promisifyAll(new GridStore(this.db, id, 'w', options));
        return store.openAsync();
      })
      .then(store => store.rewindAsync())
      .then(gridStore => gridStore.writeFileAsync(path))
      .then(doc => ({
        contentType: doc.contentType,
        fileName: doc.filename,
        fileId: doc.fileId,
        root: doc.root,
        uploadDate: doc.uploadDate,
      }))<commit_msg>Fill filename field for gridfs store.<commit_after>/**
 * Created by vedi on 11/21/13.
 */

'use strict';

const Bb = require('bluebird');
const mongoose = require('mongoose');

const GridStore = Bb.promisifyAll(mongoose.mongo.GridStore);
const ObjectID = mongoose.mongo.ObjectID;

class GridFsStorage {

  initialize(options) {
    this.db = options.dataSource.ModelClass.db.db;
  }

  getStream(fileMeta) {
    const id = fileMeta.fileId;
    const store = new GridStore(this.db, new ObjectID(id.toString()), 'r', { root: 'fs' });
    Bb.promisifyAll(store);
    return store.openAsync().then(db => ({
      contentType: fileMeta.contentType,
      contentLength: db.stream(true).totalBytesToRead,
      stream: db.stream(true),
    }));
  }

  putFile(path, options) {
    const { fileName, ...rest } = options;
    options = rest || {};
    const gridStore = Bb
      .promisifyAll(new GridStore(this.db, new ObjectID(), fileName, 'w', options));

    return Bb
      .try(() => gridStore.openAsync())
      .then(() => gridStore.writeFileAsync(path))
      .then(doc => ({
        contentType: doc.contentType,
        fileName: doc.filename,
        fileId: doc.fileId,
        root: doc.root,
        uploadDate: doc.uploadDate,
      }));
  }

  replaceFile(fileMeta, path, options) {
    const id = fileMeta.fileId;
    const { fileName, ...rest } = options;

    return Bb
      .bind(this)
      .then(() => {
        options = rest || {};
        options.root = 'fs';
        const store = Bb.promisifyAll(new GridStore(this.db, id, fileName, 'w', options));
        return store.openAsync();
      })
      .then(store => store.rewindAsync())
      .then(gridStore => gridStore.writeFileAsync(path))
      .then(doc => ({
        contentType: doc.contentType,
        fileName: doc.filename,
        fileId: doc.fileId,
        root: doc.root,
        uploadDate: doc.uploadDate,
      }))
{'fraction_non_alphanumeric': 0.15010976948408342, 'fraction_numerical': 0.003293084522502744, 'mean_word_length': 2.6781029263370333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2094095', 'n_tokens_mistral': 1188, 'n_tokens_neox': 1183, 'n_words': 242}