<commit_before>import tinify from 'tinify';
import Image from 'src/models/Image';
import s3 from 'src/config/s3';
import awsConfig from 'src/config/tinify-aws';
import { cloudFrontify, sizes } from 'src/config/constants';

export const postImage = (req, res) => {
  const source = tinify.fromBuffer(req.file.buffer);

  const original = source
    .store(awsConfig(`bolg/images/${req.body.id}`))
    .meta()
    .then((meta) => {
      const cloudFrontLink = cloudFrontify(meta.location.split('/bolg/')[1]);
      const img = new Image();
      delete img.id;
      img.downloadURL = cloudFrontLink;
      return img;
    })
    .catch((err) => {
      res.error(err);
    });

  const thumbs = Promise.all(sizes.map(size => source.resize({
    width: size.width,
  })
    .store(awsConfig(`bolg/thumbnails/${req.body.id}.${size.width}`))
    .meta()
    .then((thumbMeta) => {
      const thumbLocation = {};
      thumbLocation[size.width] = cloudFrontify(thumbMeta.location.split('/bolg/')[1]);
      return thumbLocation;
    })))
    .then(thumbsMeta => thumbsMeta.reduce((acc, thumbMeta) => Object.assign(acc, thumbMeta), {}));

  Promise.all([
    original,
    thumbs,
  ])
    .then((metas) => {
      const [image, thumbnails] = metas;
      image.thumbnails = thumbnails;
      res.send(image);
    })
    .catch((err) => {
      res.err(err);
    })
};

export const getImage = (req, res) => {
  res.send('TODO: implement this');
};

export const deleteImages = (req, res) => {
  const { id } = req.params;
  const objs = sizes.map(size => ({ Key: `thumbnails/${id}.${size.width}` }));
  objs.push({ Key: `images/${id}` });
  const deleter = s3.deleteObjects({
    Bucket: 'bolg',
    Delete: {
      Objects: objs,
    },
  });
  deleter.on('error', (err) => {
    throw err;
  });
  deleter.on('end', data => res.send(data));
}
<commit_msg>Res.error is not a function<commit_after>import tinify from 'tinify';
import Image from 'src/models/Image';
import s3 from 'src/config/s3';
import awsConfig from 'src/config/tinify-aws';
import { cloudFrontify, sizes } from 'src/config/constants';

export const postImage = (req, res) => {
  const source = tinify.fromBuffer(req.file.buffer);

  const original = source
    .store(awsConfig(`bolg/images/${req.body.id}`))
    .meta()
    .then((meta) => {
      const cloudFrontLink = cloudFrontify(meta.location.split('/bolg/')[1]);
      const img = new Image();
      delete img.id;
      img.downloadURL = cloudFrontLink;
      return img;
    })
    .catch((err) => {
      throw new Error(err);
    });

  const thumbs = Promise.all(sizes.map(size => source.resize({
    width: size.width,
  })
    .store(awsConfig(`bolg/thumbnails/${req.body.id}.${size.width}`))
    .meta()
    .then((thumbMeta) => {
      const thumbLocation = {};
      thumbLocation[size.width] = cloudFrontify(thumbMeta.location.split('/bolg/')[1]);
      return thumbLocation;
    })))
    .then(thumbsMeta => thumbsMeta.reduce((acc, thumbMeta) => Object.assign(acc, thumbMeta), {}));

  Promise.all([
    original,
    thumbs,
  ])
    .then((metas) => {
      const [image, thumbnails] = metas;
      image.thumbnails = thumbnails;
      res.send(image);
    })
    .catch((err) => {
      throw new Error(err);
    })
};

export const getImage = (req, res) => {
  res.send('TODO: implement this');
};

export const deleteImages = (req, res) => {
  const { id } = req.params;
  const objs = sizes.map(size => ({ Key: `thumbnails/${id}.${size.width}` }));
  objs.push({ Key: `images/${id}` });
  const deleter = s3.deleteObjects({
    Bucket: 'bolg',
    Delete: {
      Objects: objs,
    },
  });
  deleter.on('error', (err) => {
    throw err;
  });
  deleter.on('end', data => res.send(data));
}

{'fraction_non_alphanumeric': 0.1876338329764454, 'fraction_numerical': 0.0026766595289079227, 'mean_word_length': 3.044372294372294, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '206239', 'n_tokens_mistral': 1297, 'n_tokens_neox': 1298, 'n_words': 252}