diff --git a/.gitignore b/.gitignore index da80d8c..315e5ff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules .env www -*.DS_Store \ No newline at end of file +*.DS_Store +data \ No newline at end of file diff --git a/dist/generate.js b/dist/generate.js index f9edcde..8a9aec2 100644 --- a/dist/generate.js +++ b/dist/generate.js @@ -2,13 +2,51 @@ Object.defineProperty(exports, "__esModule", { value: true }); require("dotenv/config"); const log_1 = require("./log"); +const promises_1 = require("fs/promises"); +const path_1 = require("path"); const shell_1 = require("./shell"); class Generate { constructor() { + this.inbox = typeof process.env.INBOX !== 'undefined' ? process.env.INBOX : '~/Photos/toprocess'; this.log = (0, log_1.createLog)('generate'); this.log.info(`Generating site: ${new Date()}`); + this.generate(); + } + async generate() { + await this.checkInbox(); } async checkInbox() { + let inbox; + let images; + try { + inbox = await (0, promises_1.realpath)(this.inbox); + } + catch (err) { + this.log.error(err); + return; + } + try { + images = await (0, promises_1.readdir)(inbox); + } + catch (err) { + this.log.error(err); + return; + } + images = images.filter(el => { + if (el.toLowerCase().indexOf('.jpg') !== -1 + || el.toLowerCase().indexOf('.jpeg') !== -1 + || el.toLowerCase().indexOf('.tif') !== -1 + || el.toLowerCase().indexOf('.tiff') !== -1) { + return true; + } + return false; + }); + if (images.length === 0) { + this.log.info(`No new images found`); + return; + } + images = images.map(el => (0, path_1.join)(inbox, el)); + console.dir(images); } async img(file) { const cmd = ['bash', 'scripts/img.sh', file]; diff --git a/dist/generate.js.map b/dist/generate.js.map index 4aaf634..3ec5cf1 100644 --- a/dist/generate.js.map +++ b/dist/generate.js.map @@ -1 +1 @@ -{"version":3,"file":"generate.js","sourceRoot":"","sources":["../src/generate.ts"],"names":[],"mappings":";;AAAA,yBAAuB;AACvB,+BAAkC;AAElC,mCAAgC;AAGhC,MAAM,QAAQ;IAGb;QACC,IAAI,CAAC,GAAG,GAAG,IAAA,eAAS,EAAC,UAAU,CAAC,CAAC;QACjC,IAAI,CAAC,GAAG,CAAC,IAAI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IACpB,CAAC;IAEO,KAAK,CAAC,GAAG,CAAE,IAAa;QAC/B,MAAM,GAAG,GAAc,CAAC,MAAM,EAAE,gBAAgB,EAAE,IAAI,CAAC,CAAC;QACxD,MAAM,KAAK,GAAW,IAAI,aAAK,CAAC,GAAG,CAAC,CAAC;QACrC,IAAI,CAAC;YACJ,MAAM,KAAK,CAAC,OAAO,EAAE,CAAC;QACvB,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACd,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACpB,OAAO;QACR,CAAC;QACD,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,4BAA4B,IAAI,EAAE,CAAC,CAAC;IACnD,CAAC;CACD;AAED,IAAI,QAAQ,EAAE,CAAC"} \ No newline at end of file diff --git a/dist/hash/index.js b/dist/hash/index.js new file mode 100644 index 0000000..8a87851 --- /dev/null +++ b/dist/hash/index.js @@ -0,0 +1,17 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.hash = void 0; +const fs_1 = require("fs"); +const crypto_1 = require("crypto"); +function hash(path) { + return new Promise((resolve, reject) => { + const hashSum = (0, crypto_1.createHash)('sha256'); + const stream = (0, fs_1.createReadStream)(path); + stream.on('error', (err) => reject(err)); + stream.on('data', (chunk) => hashSum.update(chunk)); + stream.on('end', () => resolve(hashSum.digest('hex'))); + }); +} +exports.hash = hash; +module.exports = { hash }; +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/dist/hash/index.js.map b/dist/hash/index.js.map new file mode 100644 index 0000000..2985e59 --- /dev/null +++ b/dist/hash/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hash/index.ts"],"names":[],"mappings":";;;AAAA,2BAAsC;AACtC,mCAA0C;AAE1C,SAAgB,IAAI,CAAE,IAAa;IAClC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAkB,EAAE,MAAiB,EAAE,EAAE;QAC5D,MAAM,OAAO,GAAU,IAAA,mBAAU,EAAC,QAAQ,CAAC,CAAC;QAC5C,MAAM,MAAM,GAAS,IAAA,qBAAgB,EAAC,IAAI,CAAC,CAAC;QAC5C,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;QACjD,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,KAAc,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;QAC7D,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACxD,CAAC,CAAC,CAAC;AACJ,CAAC;AARD,oBAQC;AAED,MAAM,CAAC,OAAO,GAAG,EAAE,IAAI,EAAE,CAAC"} \ No newline at end of file diff --git a/sql/setup.sql b/sql/setup.sql index b3dd253..e9ba03e 100644 --- a/sql/setup.sql +++ b/sql/setup.sql @@ -1,5 +1,9 @@ CREATE TABLE IF NOT EXISTS photos { - + id TEXT PRIMARY KEY, + created INTEGER, + updated INTEGER, + + deleted INTEGER DEFAULT 0 } CREATE TABLE IF NOT EXISTS version { diff --git a/src/generate.ts b/src/generate.ts index 73eee05..07d9b33 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -1,19 +1,66 @@ import 'dotenv/config'; import { createLog } from './log'; import type { Logger } from 'winston'; -import { Shell } from './shell'; import { Database } from 'sqlite3'; +import { readFile, readdir, realpath } from 'fs/promises'; +import { join } from 'path'; +import { Shell } from './shell'; +import { hash } from './hash'; class Generate { private log : Logger; private files : string[]; + private inbox : string = typeof process.env.INBOX !== 'undefined' ? process.env.INBOX : '~/Photos/toprocess'; + constructor () { this.log = createLog('generate'); this.log.info(`Generating site: ${new Date()}`); + this.generate(); + + } + + private async generate () { + //check version + //sync + await this.checkInbox(); + //validate } private async checkInbox () { - + let inbox : string; + let images : string[]; + + try { + inbox = await realpath(this.inbox); + } catch (err) { + this.log.error(err); + return; + } + + try { + images = await readdir(inbox); + } catch (err) { + this.log.error(err); + return; + } + + images = images.filter(el => { + if (el.toLowerCase().indexOf('.jpg') !== -1 + || el.toLowerCase().indexOf('.jpeg') !== -1 + || el.toLowerCase().indexOf('.tif') !== -1 + || el.toLowerCase().indexOf('.tiff') !== -1) { + return true; + } + return false; + }); + + if (images.length === 0) { + this.log.info(`No new images found`); + return; + } + + images = images.map(el => join(inbox, el)); + console.dir(images) } private async img (file : string) { diff --git a/src/hash/index.ts b/src/hash/index.ts new file mode 100644 index 0000000..856cc41 --- /dev/null +++ b/src/hash/index.ts @@ -0,0 +1,14 @@ +import { createReadStream } from 'fs'; +import { createHash, Hash } from 'crypto'; + +export function hash (path : string) : Promise { + return new Promise((resolve : Function, reject : Function) => { + const hashSum : Hash = createHash('sha256'); + const stream : any = createReadStream(path); + stream.on('error', (err : Error) => reject(err)); + stream.on('data', (chunk : Buffer) => hashSum.update(chunk)); + stream.on('end', () => resolve(hashSum.digest('hex'))); + }); +} + +module.exports = { hash }; \ No newline at end of file