Improved site and network pages. Fixed various issues.

This commit is contained in:
2019-11-12 01:22:20 +01:00
parent 3c76d39301
commit 832e96ced1
118 changed files with 327 additions and 224 deletions

View File

@@ -19,11 +19,20 @@ function getHash(buffer) {
return hash.digest('hex');
}
async function getThumbnail(buffer) {
return sharp(buffer)
.resize({
height: config.media.thumbnailSize,
withoutEnlargement: true,
})
.toBuffer();
}
async function storePoster(release, releaseEntry) {
console.log(`Storing poster for (${release.site.name}, ${releaseEntry.id}) "${release.title}"`);
const res = await bhttp.get(release.poster);
const thumbnail = await sharp(res.body).resize({ height: config.media.thumbnailSize }).toBuffer();
const thumbnail = await getThumbnail(res.body);
if (res.statusCode === 200) {
const { pathname } = new URL(release.poster);
@@ -44,6 +53,7 @@ async function storePoster(release, releaseEntry) {
thumbnail: thumbpath,
mime: mimetype,
hash,
source: release.poster,
domain: 'releases',
target_id: releaseEntry.id,
role: 'poster',
@@ -62,32 +72,37 @@ async function storePhotos(release, releaseEntry) {
const { pathname } = new URL(photoUrl);
const mimetype = mime.getType(pathname);
const res = await bhttp.get(photoUrl);
const thumbnail = await sharp(res.body).resize({ height: config.media.thumbnailSize }).toBuffer();
try {
const res = await bhttp.get(photoUrl);
const thumbnail = await getThumbnail(res.body);
if (res.statusCode === 200) {
const extension = mime.getExtension(mimetype);
if (res.statusCode === 200) {
const extension = mime.getExtension(mimetype);
const filepath = path.join(release.site.network.slug, release.site.slug, releaseEntry.id.toString(), `${index + 1}.${extension}`);
const thumbpath = path.join(release.site.network.slug, release.site.slug, releaseEntry.id.toString(), `${index + 1}_thumb.${extension}`);
const hash = getHash(res.body);
const filepath = path.join(release.site.network.slug, release.site.slug, releaseEntry.id.toString(), `${index + 1}.${extension}`);
const thumbpath = path.join(release.site.network.slug, release.site.slug, releaseEntry.id.toString(), `${index + 1}_thumb.${extension}`);
const hash = getHash(res.body);
await Promise.all([
fs.writeFile(path.join(config.media.path, filepath), res.body),
fs.writeFile(path.join(config.media.path, thumbpath), thumbnail),
]);
await Promise.all([
fs.writeFile(path.join(config.media.path, filepath), res.body),
fs.writeFile(path.join(config.media.path, thumbpath), thumbnail),
]);
return {
filepath,
thumbpath,
mimetype,
hash,
};
return {
filepath,
thumbpath,
mimetype,
hash,
source: photoUrl,
};
}
throw new Error(`Response ${res.statusCode} not OK`);
} catch (error) {
console.warn(`Failed to store photo ${index + 1} for "${release.title}" (${photoUrl}, ${release.url}, ${release.site.name}, ${releaseEntry.id}): ${error}`);
return null;
}
console.warn(`Failed to store photo ${index + 1} for (${release.site.name}, ${releaseEntry.id}) "${release.title}": ${res.statusCode}`);
return null;
}, {
concurrency: 2,
});
@@ -99,6 +114,7 @@ async function storePhotos(release, releaseEntry) {
thumbnail: file.thumbpath,
mime: file.mimetype,
hash: file.hash,
source: file.source,
index,
domain: 'releases',
target_id: releaseEntry.id,
@@ -120,6 +136,7 @@ async function storeTrailer(release, releaseEntry) {
knex('media').insert({
path: filepath,
mime: mimetype,
source: release.trailer.src,
domain: 'releases',
target_id: releaseEntry.id,
role: 'trailer',