Deleting images deletes companion files.

and cleaner logging.
This commit is contained in:
Timothy Farrell 2017-11-19 22:52:35 -06:00
parent ad6666bd50
commit 46fdb975c5
3 changed files with 47 additions and 32 deletions

View File

@ -6,7 +6,7 @@ import { FileType } from './file.js';
class ImageSpec extends TypeSpec { class ImageSpec extends TypeSpec {
static async upload(blob) { static async upload(blob) {
const f = await FileType.upload(blob, false); const f = await FileType.upload(blob, false);
return await ImageType.getOrCreate({ const doc = await ImageType.getOrCreate({
digest: f.digest, digest: f.digest,
originalDate: f.lastModified, originalDate: f.lastModified,
importing: true, importing: true,
@ -16,6 +16,8 @@ class ImageSpec extends TypeSpec {
full: FileType.getURL(f) full: FileType.getURL(f)
} }
}); });
processImportables(doc);
return doc;
} }
static getUniqueID(doc) { static getUniqueID(doc) {
@ -25,6 +27,16 @@ class ImageSpec extends TypeSpec {
static getSequence(doc) { static getSequence(doc) {
return new Date(doc.originalDate).getTime(); return new Date(doc.originalDate).getTime();
} }
async delete(cascade = true) {
if (cascade) {
Object.keys(this.sizes).forEach(async key => {
const f = await FileType.getDocFromURL(this.sizes[key]);
f.delete();
});
}
return await this.update({ _deleted: true });
}
// //
// static validate(doc) { // static validate(doc) {
// // TODO actually validate perhaps against a JSON schema // // TODO actually validate perhaps against a JSON schema
@ -63,12 +75,7 @@ class ImageSpec extends TypeSpec {
// } // }
} }
const processImportables = backgroundTask(async function _processImportables(importables) { const processImportables = backgroundTask(async function _processImportables(image) {
if (!importables.length) {
return;
}
const image = importables[0];
const { _id, _rev } = image; const { _id, _rev } = image;
const imageData = await FileType.getFromURL(image.sizes.full); const imageData = await FileType.getFromURL(image.sizes.full);
@ -79,35 +86,39 @@ const processImportables = backgroundTask(async function _processImportables(imp
const exifData = ExifParser.create(buffer).parse(); const exifData = ExifParser.create(buffer).parse();
const { tags, imageSize } = exifData; const { tags, imageSize } = exifData;
const { width, height } = imageSize; const { width, height } = imageSize;
const { sizes, digest } = image;
const originalDate = new Date( const originalDate = new Date(
tags.DateTimeOriginal ? new Date(tags.DateTimeOriginal * 1000).toISOString() : image.originalDate tags.DateTimeOriginal ? new Date(tags.DateTimeOriginal * 1000).toISOString() : image.originalDate
).toISOString(); ).toISOString();
const img = await ImageType.getOrCreate({ await image.update(
{
originalDate, originalDate,
width, width,
height, height,
orientation: tags.Orientation, orientation: tags.Orientation,
digest: image.digest, digest,
make: tags.Make, make: tags.Make,
model: tags.Model, model: tags.Model,
flash: !!tags.Flash, flash: !!tags.Flash,
iso: tags.ISO, iso: tags.ISO,
sizes: image.sizes, sizes,
gps: { gps: {
latitude: tags.GPSLatitude, latitude: tags.GPSLatitude,
longitude: tags.GPSLongitude, longitude: tags.GPSLongitude,
altitude: tags.GPSAltitude, altitude: tags.GPSAltitude,
heading: tags.GPSImgDirection heading: tags.GPSImgDirection
} }
}); },
false
image.delete(); );
delete image.importing;
await image.save();
const module = await import('../context/generateThumbnails'); const module = await import('../context/generateThumbnails');
await module.generateThumbnailForImage(img); module.generateThumbnailForImage(image);
}, false); }, false);
export const ImageType = PouchDB.registerType('Image', ImageSpec); export const ImageType = PouchDB.registerType('Image', ImageSpec);
ImageType.find({ importing: true }, true).then(fw => fw.subscribe(processImportables)); ImageType.find({ importing: true }).then(results => results.forEach(processImportables));

View File

@ -132,7 +132,9 @@ export function PouchORM(PouchDB) {
Object.defineProperties(cls, { Object.defineProperties(cls, {
getOrCreate: { value: getOrCreate }, getOrCreate: { value: getOrCreate },
find: { value: find } find: { value: find },
db: { value: _db },
name: { value: name }
}); });
return cls; return cls;

View File

@ -8,7 +8,7 @@ export function Watcher(db, selector, include_docs) {
subscribers.add(fn); subscribers.add(fn);
if (subscribers.size === 1 && !changes) { if (subscribers.size === 1 && !changes) {
log('Watching:', db, selector); log(`Watching "${db.name}" for ${JSON.stringify(selector)}`);
changes = db changes = db
.changes({ .changes({
since: 'now', since: 'now',
@ -17,8 +17,10 @@ export function Watcher(db, selector, include_docs) {
selector selector
}) })
.on('change', change => { .on('change', change => {
log('changed:', change);
const { id, deleted, doc } = change; const { id, deleted, doc } = change;
log(
`Change from "${db.name}" for ${JSON.stringify(selector)} ${id} ${deleted ? 'deleted' : ''}`
);
subscribers.forEach(s => s(id, !!deleted, doc)); subscribers.forEach(s => s(id, !!deleted, doc));
}) })
.on('error', err => { .on('error', err => {