<commit_before>var uuid = require('node-uuid')
  , models = require('../models')
  , fs = require('fs')

exports.addModel = function(db) {
  function Attachment(params) {
    console.log('new Attachment(' + JSON.stringify(params) + ')')

    this.id = params.id

    this.ext = params.ext
    // this.filetype = params.filetype // TODO: mmmagic lib
    this.filename = params.filename
    this.path = params.path
    this.fsPath = params.fsPath
    this.postId = params.postId

    if (params.thumbnailId)
      this.thumbnailId = params.thumbnailId
    if (params.thumbnail)
      this.thumbnail = params.thumbnail
  }

  Attachment.find = function(attachmentId, callback) {
    console.log('Attachment.find("' + attachmentId + '")')
    db.hgetall('attachment:' + attachmentId, function(err, attrs) {
      // TODO: check if we find a attachment
      attrs.id = attachmentId
      var attachment = new Attachment(attrs)
      return callback(attachment)
    })
  },

  // TODO: attachmentId -> attachmentsId
  Attachment.destroy = function(attachmentId, callback) {
    console.log('Attachment.destroy("' + attachmentId + '")')
    models.Attachment.find(attachmentId, function(attachment) {
      fs.unlink(attachment.fsPath, function(err) {
        db.del('attachment:' + attachment.id, function(err, res) {
          callback(err, res)
        })
      })
    })
  }
<commit_msg>Fixes schemaless issues when image does not have fsPath field.<commit_after>var uuid = require('node-uuid')
  , models = require('../models')
  , fs = require('fs')

exports.addModel = function(db) {
  function Attachment(params) {
    console.log('new Attachment(' + JSON.stringify(params) + ')')

    this.id = params.id

    this.ext = params.ext
    // this.filetype = params.filetype // TODO: mmmagic lib
    this.filename = params.filename
    this.path = params.path
    this.fsPath = params.fsPath
    this.postId = params.postId

    if (params.thumbnailId)
      this.thumbnailId = params.thumbnailId
    if (params.thumbnail)
      this.thumbnail = params.thumbnail
  }

  Attachment.find = function(attachmentId, callback) {
    console.log('Attachment.find("' + attachmentId + '")')
    db.hgetall('attachment:' + attachmentId, function(err, attrs) {
      // TODO: check if we find a attachment
      attrs.id = attachmentId
      var attachment = new Attachment(attrs)
      return callback(attachment)
    })
  },

  // TODO: attachmentId -> attachmentsId
  Attachment.destroy = function(attachmentId, callback) {
    console.log('Attachment.destroy("' + attachmentId + '")')
    models.Attachment.find(attachmentId, function(attachment) {
      // workaround since we are schemaless
      if (attachment.fsPath) {
        fs.unlink(attachment.fsPath, function(err) {
          db.del('attachment:' + attachment.id, function(err, res) {
            callback(err, res)
          })
        })
      } else {
        db.del('attachment:' + attachment.id, function(err, res) {
          callback(err, res)
        })
      }
    })
  }

{'fraction_non_alphanumeric': 0.13480553724456162, 'fraction_numerical': 0.0, 'mean_word_length': 2.871173469387755, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2097835', 'n_tokens_mistral': 916, 'n_tokens_neox': 910, 'n_words': 185}