When a photo is too large, resize to filesize before posting to bluesky

This commit is contained in:
Matt McWilliams 2025-01-30 13:43:46 -05:00
parent 86e60cb25d
commit 905923c6f4
15 changed files with 172 additions and 6 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ node_modules
www www
*.DS_Store *.DS_Store
data data
post.log

3
dist/generate.js vendored
View File

@ -64,7 +64,8 @@ class Generate {
if (el.toLowerCase().indexOf('.jpg') !== -1 if (el.toLowerCase().indexOf('.jpg') !== -1
|| el.toLowerCase().indexOf('.jpeg') !== -1 || el.toLowerCase().indexOf('.jpeg') !== -1
|| el.toLowerCase().indexOf('.tif') !== -1 || el.toLowerCase().indexOf('.tif') !== -1
|| el.toLowerCase().indexOf('.tiff') !== -1) { || el.toLowerCase().indexOf('.tiff') !== -1
|| el.toLowerCase().indexOf('.png') !== -1) {
return true; return true;
} }
return false; return false;

File diff suppressed because one or more lines are too long

50
dist/post.js vendored
View File

@ -12,6 +12,9 @@ const argparse_1 = require("argparse");
const moment_1 = __importDefault(require("moment")); const moment_1 = __importDefault(require("moment"));
const db_1 = require("./db"); const db_1 = require("./db");
const env_1 = require("./env"); const env_1 = require("./env");
const size_1 = require("./size");
const tmp_1 = require("./tmp");
const shell_1 = require("./shell");
class Post { class Post {
constructor() { constructor() {
this.log = (0, log_1.createLog)('post'); this.log = (0, log_1.createLog)('post');
@ -44,12 +47,15 @@ ${photo.format}${photo.filmstock !== 'Unknown' ? ' - ' + photo.filmstock : ''}`;
const photo = await this.db.getBsky(this.random); const photo = await this.db.getBsky(this.random);
const identifier = (0, env_1.envString)('BSKY_USER', null); const identifier = (0, env_1.envString)('BSKY_USER', null);
const password = (0, env_1.envString)('BSKY_PASSWORD', null); const password = (0, env_1.envString)('BSKY_PASSWORD', null);
const maxSize = 976560;
let text; let text;
let post; let post;
let imagePath; let imagePath;
let imageData; let imageData;
let imageSize;
let blobResponse; let blobResponse;
let postData; let postData;
let usedTmp = false;
if (identifier === null || password === null) { if (identifier === null || password === null) {
this.log.error(`Cannot post without Bluesky username or password`); this.log.error(`Cannot post without Bluesky username or password`);
return; return;
@ -80,11 +86,39 @@ ${photo.format}${photo.filmstock !== 'Unknown' ? ' - ' + photo.filmstock : ''}`;
*/ */
this.log.info(text); this.log.info(text);
imagePath = (0, path_1.join)((0, env_1.envString)('WWW', './www'), 'img', `${photo.id}_full.jpg`); imagePath = (0, path_1.join)((0, env_1.envString)('WWW', './www'), 'img', `${photo.id}_full.jpg`);
try {
imageSize = await (0, size_1.size)(imagePath);
}
catch (err) {
this.log.error(`Error getting image size`, err);
return;
}
//976.56KB limit
if (imageSize > maxSize) {
this.log.info(`Image size too large: ${imageSize} > ${maxSize}`);
try {
imagePath = await this.imgFix(imagePath);
}
catch (err) {
this.log.error(`Error resizing image with img_fix.sh`, err);
return;
}
usedTmp = true;
try {
imageSize = await (0, size_1.size)(imagePath);
}
catch (err) {
this.log.error(`Error getting resized image size`, err);
return;
}
this.log.info(`Image resized to: ${imageSize}`);
}
try { try {
imageData = await (0, promises_1.readFile)(imagePath); imageData = await (0, promises_1.readFile)(imagePath);
} }
catch (err) { catch (err) {
this.log.error(`Error reading image data ${imagePath}`, err); this.log.error(`Error reading image data ${imagePath}`, err);
return;
} }
try { try {
blobResponse = await this.bskyAgent.uploadBlob(imageData, { contentType: 'image/jpeg' }); blobResponse = await this.bskyAgent.uploadBlob(imageData, { contentType: 'image/jpeg' });
@ -118,6 +152,22 @@ ${photo.format}${photo.filmstock !== 'Unknown' ? ' - ' + photo.filmstock : ''}`;
this.log.error(`Could not confirm that photo ${photo.name} was posted`, err); this.log.error(`Could not confirm that photo ${photo.name} was posted`, err);
return; return;
} }
if (usedTmp) {
try {
await (0, promises_1.unlink)(imagePath);
}
catch (err) {
this.log.error(`Error removing temp image file ${imagePath}`, err);
return;
}
}
}
async imgFix(imagePath) {
const dest = (0, tmp_1.tmp)('.jpg', 'bsky-upload-');
const args = ['bash', 'scripts/img_fix.sh', imagePath, dest];
const shell = new shell_1.Shell(args);
await shell.execute();
return dest;
} }
} }
new Post(); new Post();

2
dist/post.js.map vendored
View File

@ -1 +1 @@
{"version":3,"file":"post.js","sourceRoot":"","sources":["../src/post.ts"],"names":[],"mappings":";;;;;AAAA,yBAAuB;AAEvB,+BAAkC;AAElC,0CAAuC;AACvC,+BAA4B;AAC5B,sCAAyC;AAEzC,uCAA0C;AAC1C,oDAA4B;AAG5B,6BAA0B;AAE1B,+BAAkC;AAElC,MAAM,IAAI;IAMT;QALQ,QAAG,GAAY,IAAA,eAAS,EAAC,MAAM,CAAC,CAAC;QACjC,OAAE,GAAQ,IAAI,OAAE,EAAE,CAAC;QACnB,cAAS,GAAe,IAAI,eAAS,CAAC,EAAE,OAAO,EAAE,qBAAqB,EAAE,CAAC,CAAC;QAC1E,WAAM,GAAa,KAAK,CAAC;QAGhC,MAAM,MAAM,GAAG,IAAI,yBAAc,CAAC;YACjC,WAAW,EAAE,iBAAiB;SAC9B,CAAC,CAAC;QACH,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,UAAU,EAAE,EAAE,MAAM,EAAG,YAAY,EAAE,OAAO,EAAG,KAAK,EAAE,IAAI,EAAE,sBAAsB,EAAE,CAAC,CAAC;QAChH,MAAM,IAAI,GAAS,MAAM,CAAC,UAAU,EAAE,CAAC;QACvC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,IAAI,CAAC,IAAI,EAAE,CAAC;IACb,CAAC;IAEO,KAAK,CAAC,IAAI;QACjB,MAAM,IAAI,CAAC,IAAI,EAAE,CAAC;IACnB,CAAC;IAEO,eAAe,CAAE,KAAa;QACrC,IAAI,GAAG,GAAY,GAAG,KAAK,CAAC,WAAW;;EAEvC,KAAK,CAAC,MAAM,GAAG,KAAK,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,GAAG,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;QAC9E,MAAM,OAAO,GAAY,gBAAM,CAAC,IAAI,CAAC,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QAEtC,MAAM,KAAK,CAAC,OAAO,EAAE,CAAC;QAEtB,OAAO,IAAI,CAAC;IACb,CAAC;CACD;AAED,IAAI,IAAI,EAAE,CAAC"}

10
dist/size/index.js vendored Normal file
View File

@ -0,0 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.size = size;
const promises_1 = require("fs/promises");
async function size(filePath) {
const stats = await (0, promises_1.stat)(filePath);
return stats.size;
}
module.exports = { size };
//# sourceMappingURL=index.js.map

1
dist/size/index.js.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/size/index.ts"],"names":[],"mappings":";;AAGA,oBAGC;AAND,0CAAmC;AAG5B,KAAK,UAAU,IAAI,CAAE,QAAiB;IAC5C,MAAM,KAAK,GAAW,MAAM,IAAA,eAAI,EAAC,QAAQ,CAAC,CAAC;IAC3C,OAAO,KAAK,CAAC,IAAI,CAAC;AACnB,CAAC;AAED,MAAM,CAAC,OAAO,GAAG,EAAE,IAAI,EAAE,CAAC"}

17
dist/tmp/index.js vendored Normal file
View File

@ -0,0 +1,17 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tmp = tmp;
const os_1 = require("os");
const path_1 = require("path");
const crypto_1 = require("crypto");
const TMP = (0, os_1.tmpdir)();
function tmp(ext = '', prefix = '3d-tmp-', fileName = null) {
let path = null;
if (fileName === null) {
fileName = prefix + (0, crypto_1.randomBytes)(32).toString('hex') + ext;
}
path = (0, path_1.join)(TMP, fileName);
return path;
}
module.exports = { tmp };
//# sourceMappingURL=index.js.map

1
dist/tmp/index.js.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tmp/index.ts"],"names":[],"mappings":";;AAMA,kBAOC;AAbD,2BAA4B;AAC5B,+BAA4B;AAC5B,mCAAqC;AAErC,MAAM,GAAG,GAAY,IAAA,WAAM,GAAE,CAAC;AAE9B,SAAgB,GAAG,CAAE,MAAe,EAAE,EAAE,SAAkB,SAAS,EAAE,WAAoB,IAAI;IAC5F,IAAI,IAAI,GAAY,IAAI,CAAC;IACzB,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;QACvB,QAAQ,GAAG,MAAM,GAAG,IAAA,oBAAW,EAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC;IAC3D,CAAC;IACD,IAAI,GAAG,IAAA,WAAI,EAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;IAC3B,OAAO,IAAI,CAAC;AACb,CAAC;AAED,MAAM,CAAC,OAAO,GAAG,EAAE,GAAG,EAAE,CAAC"}

View File

@ -28,7 +28,7 @@ for sizeRaw in ${SIZES[@]}; do
name=${name%.*} name=${name%.*}
output="${WWW}/img/${ID}_${sizeName}.jpg" output="${WWW}/img/${ID}_${sizeName}.jpg"
img "${1}" "${output}" "${size}" img "${1}" "${output}" "${size}"
if [[ -f "${WWW}/img/${ID}_${sizeName}-1.jpg" ]]; then if [[ -f "${WWW}/img/${ID}_${sizeName}-0.jpg" ]]; then
mv "${WWW}/img/${ID}_${sizeName}-0.jpg" "${WWW}/img/${ID}_${sizeName}.jpg" mv "${WWW}/img/${ID}_${sizeName}-0.jpg" "${WWW}/img/${ID}_${sizeName}.jpg"
rm "${WWW}/img/${ID}_${sizeName}-1.jpg" rm "${WWW}/img/${ID}_${sizeName}-1.jpg"
fi fi

3
scripts/img_fix.sh Normal file
View File

@ -0,0 +1,3 @@
#!/bin/bash
convert "${1}" -define jpeg:extent=975kb "${2}"

View File

@ -84,7 +84,8 @@ class Generate {
if (el.toLowerCase().indexOf('.jpg') !== -1 if (el.toLowerCase().indexOf('.jpg') !== -1
|| el.toLowerCase().indexOf('.jpeg') !== -1 || el.toLowerCase().indexOf('.jpeg') !== -1
|| el.toLowerCase().indexOf('.tif') !== -1 || el.toLowerCase().indexOf('.tif') !== -1
|| el.toLowerCase().indexOf('.tiff') !== -1) { || el.toLowerCase().indexOf('.tiff') !== -1
|| el.toLowerCase().indexOf('.png') !== -1) {
return true; return true;
} }
return false; return false;

View File

@ -2,7 +2,7 @@ import 'dotenv/config';
import { createLog } from './log'; import { createLog } from './log';
import type { Logger } from 'winston'; import type { Logger } from 'winston';
import { readFile } from 'fs/promises'; import { readFile, unlink } from 'fs/promises';
import { join } from 'path'; import { join } from 'path';
import { BskyAgent } from '@atproto/api'; import { BskyAgent } from '@atproto/api';
import type CallOptions from '@atproto/api'; import type CallOptions from '@atproto/api';
@ -13,6 +13,9 @@ import type { Moment } from 'moment';
import { DB } from './db'; import { DB } from './db';
import type { Photo } from './db'; import type { Photo } from './db';
import { envString } from './env'; import { envString } from './env';
import { size } from './size';
import { tmp } from './tmp';
import { Shell } from './shell';
class Post { class Post {
private log : Logger = createLog('post'); private log : Logger = createLog('post');
@ -50,12 +53,15 @@ ${photo.format}${photo.filmstock !== 'Unknown' ? ' - ' + photo.filmstock : ''}`;
const photo : Photo = await this.db.getBsky(this.random); const photo : Photo = await this.db.getBsky(this.random);
const identifier : string = envString('BSKY_USER', null); const identifier : string = envString('BSKY_USER', null);
const password : string = envString('BSKY_PASSWORD', null); const password : string = envString('BSKY_PASSWORD', null);
const maxSize : number = 976560;
let text : string; let text : string;
let post : any; let post : any;
let imagePath : string; let imagePath : string;
let imageData : Buffer; let imageData : Buffer;
let imageSize : number;
let blobResponse : any; let blobResponse : any;
let postData : any; let postData : any;
let usedTmp : boolean = false;
if (identifier === null || password === null) { if (identifier === null || password === null) {
this.log.error(`Cannot post without Bluesky username or password`); this.log.error(`Cannot post without Bluesky username or password`);
@ -91,10 +97,41 @@ ${photo.format}${photo.filmstock !== 'Unknown' ? ' - ' + photo.filmstock : ''}`;
imagePath = join(envString('WWW', './www'), 'img', `${photo.id}_full.jpg`); imagePath = join(envString('WWW', './www'), 'img', `${photo.id}_full.jpg`);
try {
imageSize = await size(imagePath);
} catch (err) {
this.log.error(`Error getting image size`, err);
return;
}
//976.56KB limit
if (imageSize > maxSize) {
this.log.info(`Image size too large: ${imageSize} > ${maxSize}`);
try {
imagePath = await this.imgFix(imagePath);
} catch (err) {
this.log.error(`Error resizing image with img_fix.sh`, err);
return;
}
usedTmp = true;
try {
imageSize = await size(imagePath);
} catch (err) {
this.log.error(`Error getting resized image size`, err);
return;
}
this.log.info(`Image resized to: ${imageSize}`);
}
try { try {
imageData = await readFile(imagePath); imageData = await readFile(imagePath);
} catch (err) { } catch (err) {
this.log.error(`Error reading image data ${imagePath}`, err); this.log.error(`Error reading image data ${imagePath}`, err);
return;
} }
try { try {
@ -130,6 +167,25 @@ ${photo.format}${photo.filmstock !== 'Unknown' ? ' - ' + photo.filmstock : ''}`;
return; return;
} }
if (usedTmp) {
try {
await unlink(imagePath);
} catch (err) {
this.log.error(`Error removing temp image file ${imagePath}`, err);
return;
}
}
}
private async imgFix (imagePath : string) : Promise<string> {
const dest : string = tmp('.jpg', 'bsky-upload-');
const args : string[] = ['bash', 'scripts/img_fix.sh', imagePath, dest];
const shell : Shell = new Shell(args);
await shell.execute();
return dest;
} }
} }

9
src/size/index.ts Normal file
View File

@ -0,0 +1,9 @@
import { stat } from 'fs/promises';
import type { Stats } from 'fs';
export async function size (filePath : string) : Promise<number> {
const stats : Stats = await stat(filePath);
return stats.size;
}
module.exports = { size };

16
src/tmp/index.ts Normal file
View File

@ -0,0 +1,16 @@
import { tmpdir } from 'os';
import { join } from 'path';
import { randomBytes } from 'crypto';
const TMP : string = tmpdir();
export function tmp (ext : string = '', prefix : string = '3d-tmp-', fileName : string = null) : string {
let path : string = null;
if (fileName === null) {
fileName = prefix + randomBytes(32).toString('hex') + ext;
}
path = join(TMP, fileName);
return path;
}
module.exports = { tmp };