Auto-detect mimetype from file's content
This commit is contained in:
16
index.mjs
16
index.mjs
@@ -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}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user