Auto-detect mimetype from file's content

This commit is contained in:
Elizabeth Cray
2025-01-31 23:30:38 -05:00
parent db6bf23fb5
commit 5487f633a4
3 changed files with 151 additions and 6 deletions

View File

@@ -22,6 +22,7 @@ import { htmlToText } from "html-to-text";
import axios from "axios";
import { AtpAgent, RichText } from '@atproto/api'
import { v4 as uuid } from 'uuid';
import { fileTypeFromBuffer } from 'file-type';
const historyFile = process.env.HISTORY_FILE || '.history';
let history = [];
@@ -90,12 +91,14 @@ if (existsSync(historyFile)){
const bskyPost = async (text, media = []) => {
let uploadedMedia = [];
for (let m of media){
let mime = `${m.url}`.split('.').pop();
mime = mime.toLowerCase();
mime = mime === 'jpg'?'jpeg':mime;
// let mime = `${m.url}`.split('.').pop();
// mime = mime.toLowerCase();
// mime = mime === 'jpg'?'jpeg':mime;
const fileBuffer = Buffer.from(readFileSync(m.data));
const { ext, mimeT } = await fileTypeFromBuffer(fileBuffer);
let uploadResult = await bsky.uploadBlob(fileBuffer, {
encoding: `image/${mime}`
// encoding: `image/${mime}`
encoding: mimeT
});
if (uploadResult.success){
uploadedMedia.push({
@@ -117,7 +120,6 @@ const bskyPost = async (text, media = []) => {
createdAt: new Date().toISOString()
};
if (uploadedMedia.length > 0){
// TODO: Add aspect ratio to images
post.embed = {
"$type": "app.bsky.embed.images",
images: uploadedMedia
@@ -191,7 +193,9 @@ client.get(`/accounts/${process.env.MASTODON_ID}/statuses`, {
}
}else{
// is boosted post
// TODO: Handle boosts
let text = status.reblog.url;
bskyPost(text, []);
appendFileSync(historyFile, `\n${status.id}`);
}
}
}