Skip to content

Commit

Permalink
chore(service): remove unreferenced legacy attachment api code
Browse files Browse the repository at this point in the history
  • Loading branch information
restjohn committed Aug 6, 2024
1 parent 526a88d commit a2474e5
Showing 1 changed file with 5 additions and 81 deletions.
86 changes: 5 additions & 81 deletions service/src/api/attachment.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,15 @@
const ObservationModel = require('../models/observation')
, log = require('winston')
, path = require('path')
, fs = require('fs-extra')
, environment = require('../environment/env');
const log = require('winston')
const path = require('path')
const fs = require('fs-extra')
const environment = require('../environment/env')

const attachmentBase = environment.attachmentBaseDirectory;

const createAttachmentPath = function(event) {
const now = new Date();
return path.join(
event.collectionName,
now.getFullYear().toString(),
(now.getMonth() + 1).toString(),
now.getDate().toString()
);
};
const attachmentBase = environment.attachmentBaseDirectory

function Attachment(event, observation) {
this._event = event;
this._observation = observation;
}

Attachment.prototype.getById = function(attachmentId, options, callback) {
const size = options.size ? Number(options.size) : null;

ObservationModel.getAttachment(this._event, this._observation._id, attachmentId, function(err, attachment) {
if (!attachment) return callback(err);

if (size) {
attachment.thumbnails.forEach(function(thumbnail) {
if ((thumbnail.minDimension < attachment.height || !attachment.height) &&
(thumbnail.minDimension < attachment.width || !attachment.width) &&
(thumbnail.minDimension >= size)) {
attachment = thumbnail;
}
});
}

if (attachment && attachment.relativePath) attachment.path = path.join(attachmentBase, attachment.relativePath);

callback(null, attachment);
});
};

Attachment.prototype.update = function(attachmentId, attachment, callback) {
const relativePath = createAttachmentPath(this._event);
// move file upload to its new home
const dir = path.join(attachmentBase, relativePath);
fs.mkdirp(dir, err => {
if (err) return callback(err);

const fileName = path.basename(attachment.path);
attachment.relativePath = path.join(relativePath, fileName);
const file = path.join(attachmentBase, attachment.relativePath);

fs.move(attachment.path, file, err => {
if (err) return callback(err);

ObservationModel.addAttachment(this._event, this._observation._id, attachmentId, attachment, (err, newAttachment) => {
// TODO: now defunct after removing legacy attachment events module
// if (!err && newAttachment) {
// EventEmitter.emit(AttachmentEvents.events.add, newAttachment.toObject(), this._observation, this._event);
// }
callback(err, newAttachment);
});
});
});
};

Attachment.prototype.delete = function(attachmentId, callback) {
const attachment = this._observation.attachments.find(attachment => attachment._id.toString() === attachmentId);
ObservationModel.removeAttachment(this._event, this._observation._id, attachmentId, err => {
if (err) return callback(err);

if (attachment && attachment.relativePath) {
const file = path.join(attachmentBase, attachment.relativePath);
fs.remove(file, err => {
if (err) {
log.error('Could not remove attachment file ' + file + '.', err);
}
});
}

callback();
});
};

/**
* TODO: this no longer works with the directory scheme `FileSystemAttachmentStore` uses.
*/
Expand Down

0 comments on commit a2474e5

Please sign in to comment.