photosite/dist/generate.js

180 lines
5.9 KiB
JavaScript
Raw Normal View History

"use strict";
2024-12-03 04:09:32 +00:00
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
2024-01-08 20:10:39 +00:00
require("dotenv/config");
const log_1 = require("./log");
2024-01-08 20:38:27 +00:00
const promises_1 = require("fs/promises");
const path_1 = require("path");
2024-12-03 04:09:32 +00:00
const sizeOf = __importStar(require("image-size"));
2024-01-08 20:10:39 +00:00
const shell_1 = require("./shell");
2024-12-03 04:09:32 +00:00
const files3_1 = require("./files3");
const env_1 = require("./env");
const db_1 = require("./db");
class Generate {
constructor() {
2024-12-03 04:09:32 +00:00
this.inbox = (0, env_1.envString)('INBOX', '~/Photos/toprocess');
this.photos = (0, env_1.envString)('PHOTOS', '~/Photos/processed');
this.log = (0, log_1.createLog)('generate');
this.log.info(`Generating site: ${new Date()}`);
2024-12-03 04:09:32 +00:00
this.db = new db_1.DB();
this.s3 = new files3_1.Files3((0, env_1.envString)('S3_BUCKET', 'mmcwilliamsphotos'), true);
2024-01-08 20:38:27 +00:00
this.generate();
}
async generate() {
2024-12-03 04:09:32 +00:00
//check version
//sync
2024-01-08 20:38:27 +00:00
await this.checkInbox();
2024-12-03 04:09:32 +00:00
//validate
}
2024-01-08 20:10:39 +00:00
async checkInbox() {
2024-01-08 20:38:27 +00:00
let inbox;
let images;
2024-12-03 04:09:32 +00:00
let filename;
let meta;
let dimensions;
2024-01-08 20:38:27 +00:00
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;
}
2024-12-03 04:09:32 +00:00
images = images.filter((el) => {
2024-01-08 20:38:27 +00:00
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;
}
2024-12-03 04:09:32 +00:00
images = await Promise.all(images.map(async (el) => {
return await (0, promises_1.realpath)((0, path_1.join)(inbox, el));
}));
for (let image of images) {
this.log.info(image);
filename = (0, path_1.basename)(image);
meta = this.parseFilename(filename);
dimensions = await this.getImageDimensions(image);
meta.width = dimensions.width;
meta.height = dimensions.height;
console.dir(meta);
}
2024-01-08 20:10:39 +00:00
}
2024-12-03 04:09:32 +00:00
async img(file, exif) {
const cmd = ['bash', 'scripts/img.sh', file, exif];
2024-01-08 20:10:39 +00:00
const shell = new shell_1.Shell(cmd);
try {
await shell.execute();
}
catch (err) {
this.log.error(err);
return;
}
this.log.info(`Processed image file for ${file}`);
}
2024-12-03 04:09:32 +00:00
async getImageDimensions(imagePath) {
let dimensions;
try {
dimensions = await sizeOf(imagePath);
return dimensions;
}
catch (err) {
this.log.error('Error getting image dimensions:', err);
}
}
capitalize(str) {
return (str.substring(0, 1).toUpperCase()) + str.substring(1);
}
formatProperNouns(str) {
let parts = str.split('-');
parts = parts.map(el => this.capitalize(el));
return parts.join(' ');
}
//year
//month
//day
//format
//filmstock
//location
//description
//original
//2024_12_02_35mm_Kodak-Gold-200_Somerville-MA_Walk-with-Charlie#000061280009.tif
parseFilename(filename) {
const halves = filename.split('#');
const parts = halves[0].split('');
let meta = {};
for (let i = 0; i < parts.length; i++) {
switch (i) {
case 0:
meta.year = parseInt(parts[i]);
break;
case 1:
meta.month = parseInt(parts[i]);
break;
case 2:
meta.day = parseInt(parts[i]);
break;
case 3:
meta.format = parts[i];
break;
case 4:
meta.filmstock = parts[i].split('-').join(' ');
break;
case 5:
meta.location = parts[i].split('-').join(' ');
break;
case 6:
meta.description = parts[i].split('-').join(' ');
break;
}
}
meta.original = halves[1];
return meta;
}
}
new Generate();
//# sourceMappingURL=generate.js.map