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 {
static async upload(blob) {
const f = await FileType.upload(blob, false);
return await ImageType.getOrCreate({
const doc = await ImageType.getOrCreate({
digest: f.digest,
originalDate: f.lastModified,
importing: true,
@ -16,6 +16,8 @@ class ImageSpec extends TypeSpec {
full: FileType.getURL(f)
}
});
processImportables(doc);
return doc;
}
static getUniqueID(doc) {
@ -25,6 +27,16 @@ class ImageSpec extends TypeSpec {
static getSequence(doc) {
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) {
// // TODO actually validate perhaps against a JSON schema
@ -63,12 +75,7 @@ class ImageSpec extends TypeSpec {
// }
}
const processImportables = backgroundTask(async function _processImportables(importables) {
if (!importables.length) {
return;
}
const image = importables[0];
const processImportables = backgroundTask(async function _processImportables(image) {
const { _id, _rev } = image;
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 { tags, imageSize } = exifData;
const { width, height } = imageSize;
const { sizes, digest } = image;
const originalDate = new Date(
tags.DateTimeOriginal ? new Date(tags.DateTimeOriginal * 1000).toISOString() : image.originalDate
).toISOString();
const img = await ImageType.getOrCreate({
await image.update(
{
originalDate,
width,
height,
orientation: tags.Orientation,
digest: image.digest,
digest,
make: tags.Make,
model: tags.Model,
flash: !!tags.Flash,
iso: tags.ISO,
sizes: image.sizes,
sizes,
gps: {
latitude: tags.GPSLatitude,
longitude: tags.GPSLongitude,
altitude: tags.GPSAltitude,
heading: tags.GPSImgDirection
}
});
image.delete();
},
false
);
delete image.importing;
await image.save();
const module = await import('../context/generateThumbnails');
await module.generateThumbnailForImage(img);
module.generateThumbnailForImage(image);
}, false);
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, {
getOrCreate: { value: getOrCreate },
find: { value: find }
find: { value: find },
db: { value: _db },
name: { value: name }
});
return cls;

View File

@ -8,7 +8,7 @@ export function Watcher(db, selector, include_docs) {
subscribers.add(fn);
if (subscribers.size === 1 && !changes) {
log('Watching:', db, selector);
log(`Watching "${db.name}" for ${JSON.stringify(selector)}`);
changes = db
.changes({
since: 'now',
@ -17,8 +17,10 @@ export function Watcher(db, selector, include_docs) {
selector
})
.on('change', change => {
log('changed:', 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));
})
.on('error', err => {