<commit_before>import path from 'path';
import _debug from 'debug';
import uuid from 'uuid/v4';
import * as objection from 'objection';
import fs from 'fs-extra';
import shortId from 'shortid';
import Busboy from 'busboy';
import { responseHandler, BadRequest } from '../../core/index';
import Activity from '../../models/activity';
import Attachment from '../../models/attachment';

const debug = _debug('boldrAPI:attachment');
const gm = require('gm').subClass({ imageMagick: true });

const regex = new RegExp('^.*.((j|J)(p|P)(e|E)?(g|G)|(g|G)(i|I)(f|F)|(p|P)(n|N)(g|G))$');
/**
 * Returns a list of all attachments
 * @method listAttachments
 * @param  {Object}        req  the request object
 * @param  {Object}        res  the response object
 * @return {Promise}            the array of attachment objects
 */
export async function listAttachments(req, res, next) {
  try {
    const medias = await Attachment.query();

    return responseHandler(res, 200, medias);
  } catch (error) {
    /* istanbul ignore next */
    return next(error);
  }
}

/**
 * Returns a specific attachment and its data
 * @method getAttachment
 * @param  {Object}        req  the request object
 * @param  {Object}        res  the response object
 * @return {Promise}         the attachment object
 */
export async function getAttachment(req, res, next) {
  try {
    const file = await Attachment.query().findById(req.params.id);
    return responseHandler(res, 200, file);
  } catch (err) {
    /* istanbul ignore next */
    return next(error);
  }
}

/**
 * Update an attachment in the database.
 * @method updateAttachment
 * @param  {Object}        req  the request object
 * @param  {Object}        res  the response object
 * @return {Object}             returns the response
 */
export async function updateAttachment(req, res, next) {
  try {
    const updatedAttachment = await Attachment.query().patchAndFetchById(req.params.id, req.body);

    await Activity.query().insert({
      userId: req.user.id,
      type: 'update',
      activityAttachment: req.params.id,
    });

    return responseHandler(res, 202, updatedAttachment);
  } catch (error) {
    /* istanbul ignore next */
    return next(error);
  }
}

/**
 * Delete an attachment from the database and file system.
 * @method deleteAttachment
 * @param  {Object}        req  the request object
 * @param  {Object}        res  the response object
 * @param  {Function}       next move to the next middleware
 * @return {Promise}             Promise that the file deleted
 */
export async function deleteAttachment(req, res, next) {
  try {
    // query for the requested attachment
    const attachment = await Attachment.query().findById(req.params.id);
    // return a bad request if we cannot locate
    if (!attachment) {
      return next(new BadRequest());
    }
    // unlink the attachment from the activity
    await Activity.query().delete().where({ activityAttachment: req.params.id }).first();

    // remove the attachment from the database
    await Attachment.query().deleteById(req.params.id);
    // remove from the file system.
    fs.removeSync(`./public/files/${attachment.safeName}`);
    // send a 204<commit_msg>update path for uploaded files<commit_after>import path from 'path';
import _debug from 'debug';
import uuid from 'uuid/v4';
import * as objection from 'objection';
import fs from 'fs-extra';
import shortId from 'shortid';
import Busboy from 'busboy';
import { responseHandler, BadRequest } from '../../core/index';
import Activity from '../../models/activity';
import Attachment from '../../models/attachment';

const debug = _debug('boldrAPI:attachment');
const gm = require('gm').subClass({ imageMagick: true });

const regex = new RegExp('^.*.((j|J)(p|P)(e|E)?(g|G)|(g|G)(i|I)(f|F)|(p|P)(n|N)(g|G))$');
/**
 * Returns a list of all attachments
 * @method listAttachments
 * @param  {Object}        req  the request object
 * @param  {Object}        res  the response object
 * @return {Promise}            the array of attachment objects
 */
export async function listAttachments(req, res, next) {
  try {
    const medias = await Attachment.query();

    return responseHandler(res, 200, medias);
  } catch (error) {
    /* istanbul ignore next */
    return next(error);
  }
}

/**
 * Returns a specific attachment and its data
 * @method getAttachment
 * @param  {Object}        req  the request object
 * @param  {Object}        res  the response object
 * @return {Promise}         the attachment object
 */
export async function getAttachment(req, res, next) {
  try {
    const file = await Attachment.query().findById(req.params.id);
    return responseHandler(res, 200, file);
  } catch (err) {
    /* istanbul ignore next */
    return next(error);
  }
}

/**
 * Update an attachment in the database.
 * @method updateAttachment
 * @param  {Object}        req  the request object
 * @param  {Object}        res  the response object
 * @return {Object}             returns the response
 */
export async function updateAttachment(req, res, next) {
  try {
    const updatedAttachment = await Attachment.query().patchAndFetchById(req.params.id, req.body);

    await Activity.query().insert({
      userId: req.user.id,
      type: 'update',
      activityAttachment: req.params.id,
    });

    return responseHandler(res, 202, updatedAttachment);
  } catch (error) {
    /* istanbul ignore next */
    return next(error);
  }
}

/**
 * Delete an attachment from the database and file system.
 * @method deleteAttachment
 * @param  {Object}        req  the request object
 * @param  {Object}        res  the response object
 * @param  {Function}       next move to the next middleware
 * @return {Promise}             Promise that the file deleted
 */
export async function deleteAttachment(req, res, next) {
  try {
    // query for the requested attachment
    const attachment = await Attachment.query().findById(req.params.id);
    // return a bad request if we cannot locate
    if (!attachment) {
      return next(new BadRequest());
    }
    // unlink the attachment from the activity
    await Activity.query().delete().where({ activityAttachment: req.params.id }).first();

    // remove the attachment from the database
    await Attachment.query().deleteById(req.params.id);
    // remove from the file system.
    fs.removeSync(`./static/uploads/${attachment.safeName}`);
    // send a 204
{'fraction_non_alphanumeric': 0.12825713839912622, 'fraction_numerical': 0.004056795131845842, 'mean_word_length': 3.143503555268261, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4221424', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1838, 'n_words': 609}