2023-10-26 17:49:46 -05:00
|
|
|
const { S3 } = require("@aws-sdk/client-s3");
|
2023-10-25 14:42:36 -05:00
|
|
|
const { PrismaClient } = require("@prisma/client");
|
|
|
|
const { existsSync } = require("fs");
|
2023-10-26 17:49:46 -05:00
|
|
|
const util = require("util");
|
2023-10-25 14:42:36 -05:00
|
|
|
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
|
|
|
|
const STORAGE_FOLDER = process.env.STORAGE_FOLDER || "data";
|
|
|
|
|
2023-10-26 17:49:46 -05:00
|
|
|
const s3Client =
|
|
|
|
process.env.SPACES_ENDPOINT &&
|
|
|
|
process.env.SPACES_REGION &&
|
|
|
|
process.env.SPACES_KEY &&
|
|
|
|
process.env.SPACES_SECRET
|
|
|
|
? new S3({
|
|
|
|
forcePathStyle: false,
|
|
|
|
endpoint: process.env.SPACES_ENDPOINT,
|
|
|
|
region: process.env.SPACES_REGION,
|
|
|
|
credentials: {
|
|
|
|
accessKeyId: process.env.SPACES_KEY,
|
|
|
|
secretAccessKey: process.env.SPACES_SECRET,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
: undefined;
|
|
|
|
|
2023-10-25 14:42:36 -05:00
|
|
|
async function checkFileExistence(path) {
|
2023-10-26 17:49:46 -05:00
|
|
|
if (s3Client) {
|
|
|
|
const bucketParams = {
|
|
|
|
Bucket: process.env.BUCKET_NAME,
|
|
|
|
Key: path,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const headObjectAsync = util.promisify(
|
|
|
|
s3Client.headObject.bind(s3Client)
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await headObjectAsync(bucketParams);
|
|
|
|
return true;
|
|
|
|
} catch (err) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.log("Error:", err);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
if (existsSync(STORAGE_FOLDER + "/" + path)) {
|
|
|
|
return true;
|
|
|
|
} else return false;
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
2023-10-25 14:42:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avatars
|
2023-10-26 17:49:46 -05:00
|
|
|
async function migrateToV2() {
|
2023-10-25 14:42:36 -05:00
|
|
|
const users = await prisma.user.findMany();
|
|
|
|
|
|
|
|
for (let user of users) {
|
2023-10-26 17:49:46 -05:00
|
|
|
const path = `uploads/avatar/${user.id}.jpg`;
|
2023-10-25 14:42:36 -05:00
|
|
|
|
|
|
|
const res = await checkFileExistence(path);
|
|
|
|
|
|
|
|
if (res) {
|
2023-10-26 17:49:46 -05:00
|
|
|
await prisma.user.update({
|
|
|
|
where: { id: user.id },
|
2023-10-27 23:45:14 -05:00
|
|
|
data: { image: path },
|
2023-10-26 17:49:46 -05:00
|
|
|
});
|
|
|
|
console.log(`Updated avatar for avatar ${user.id}`);
|
2023-10-25 14:42:36 -05:00
|
|
|
} else {
|
2023-10-26 17:49:46 -05:00
|
|
|
console.log(`No avatar found for avatar ${user.id}`);
|
2023-10-25 14:42:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const links = await prisma.link.findMany();
|
|
|
|
|
2023-10-26 17:49:46 -05:00
|
|
|
// PDFs
|
2023-10-25 14:42:36 -05:00
|
|
|
for (let link of links) {
|
2023-10-26 17:49:46 -05:00
|
|
|
const path = `archives/${link.collectionId}/${link.id}.pdf`;
|
2023-10-25 14:42:36 -05:00
|
|
|
|
|
|
|
const res = await checkFileExistence(path);
|
|
|
|
|
|
|
|
if (res) {
|
2023-10-26 17:49:46 -05:00
|
|
|
await prisma.link.update({
|
|
|
|
where: { id: link.id },
|
|
|
|
data: { pdfPath: path },
|
|
|
|
});
|
|
|
|
console.log(`Updated capture for pdf ${link.id}`);
|
2023-10-25 14:42:36 -05:00
|
|
|
} else {
|
2023-10-26 17:49:46 -05:00
|
|
|
console.log(`No capture found for pdf ${link.id}`);
|
2023-10-25 14:42:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:49:46 -05:00
|
|
|
// Screenshots
|
2023-10-25 14:42:36 -05:00
|
|
|
for (let link of links) {
|
2023-10-26 17:49:46 -05:00
|
|
|
const path = `archives/${link.collectionId}/${link.id}.png`;
|
2023-10-25 14:42:36 -05:00
|
|
|
|
|
|
|
const res = await checkFileExistence(path);
|
|
|
|
|
|
|
|
if (res) {
|
2023-10-26 17:49:46 -05:00
|
|
|
await prisma.link.update({
|
|
|
|
where: { id: link.id },
|
|
|
|
data: { screenshotPath: path },
|
|
|
|
});
|
|
|
|
console.log(`Updated capture for screenshot ${link.id}`);
|
2023-10-25 14:42:36 -05:00
|
|
|
} else {
|
2023-10-26 17:49:46 -05:00
|
|
|
console.log(`No capture found for screenshot ${link.id}`);
|
2023-10-25 14:42:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await prisma.$disconnect();
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:49:46 -05:00
|
|
|
migrateToV2().catch((e) => {
|
2023-10-25 14:42:36 -05:00
|
|
|
console.error(e);
|
|
|
|
process.exit(1);
|
|
|
|
});
|