Storing image dimensions and file size to database. Added new site Filthy Femdom to Kink.
This commit is contained in:
41
src/media.js
41
src/media.js
@@ -62,11 +62,17 @@ function pickQuality(items) {
|
||||
return item || items[0];
|
||||
}
|
||||
|
||||
async function getEntropy(buffer) {
|
||||
async function getMeta(buffer) {
|
||||
try {
|
||||
const { entropy } = await sharp(buffer).stats();
|
||||
const { width, height, size } = await sharp(buffer).metadata();
|
||||
|
||||
return entropy;
|
||||
return {
|
||||
width,
|
||||
height,
|
||||
size,
|
||||
entropy,
|
||||
};
|
||||
} catch (error) {
|
||||
logger.warn(`Failed to retrieve image entropy, using 7.5: ${error.message}`);
|
||||
|
||||
@@ -125,7 +131,7 @@ async function fetchItem(source, index, existingItemsBySource, domain, role, att
|
||||
const mimetype = mime.getType(pathname);
|
||||
const extension = mime.getExtension(mimetype);
|
||||
const hash = getHash(res.body);
|
||||
const entropy = /image/.test(mimetype) ? await getEntropy(res.body) : null;
|
||||
const { entropy, size, width, height } = /image/.test(mimetype) ? await getMeta(res.body) : {};
|
||||
|
||||
logger.verbose(`Fetched media item from ${source.src || source}`);
|
||||
|
||||
@@ -135,6 +141,9 @@ async function fetchItem(source, index, existingItemsBySource, domain, role, att
|
||||
extension,
|
||||
hash,
|
||||
entropy,
|
||||
size,
|
||||
width,
|
||||
height,
|
||||
quality: source.quality || null,
|
||||
source: originalSource?.src || originalSource || source.src || source,
|
||||
scraper: source.scraper,
|
||||
@@ -192,10 +201,13 @@ async function saveItems(items, domain, role) {
|
||||
mimetype: item.mimetype,
|
||||
extension: item.extension,
|
||||
hash: item.hash,
|
||||
size: item.size,
|
||||
width: item.width,
|
||||
height: item.height,
|
||||
quality: item.quality,
|
||||
entropy: item.entropy,
|
||||
scraper: item.scraper,
|
||||
copyright: item.copyright,
|
||||
quality: item.quality,
|
||||
source: item.source,
|
||||
};
|
||||
}
|
||||
@@ -207,9 +219,12 @@ async function saveItems(items, domain, role) {
|
||||
mimetype: item.mimetype,
|
||||
extension: item.extension,
|
||||
hash: item.hash,
|
||||
size: item.size,
|
||||
width: item.width,
|
||||
height: item.height,
|
||||
quality: item.quality,
|
||||
entropy: item.entropy,
|
||||
scraper: item.scraper,
|
||||
quality: item.quality,
|
||||
copyright: item.copyright,
|
||||
source: item.source,
|
||||
};
|
||||
@@ -226,6 +241,10 @@ function curateItemEntries(items) {
|
||||
thumbnail: item.thumbpath,
|
||||
mime: item.mimetype,
|
||||
hash: item.hash,
|
||||
size: item.size,
|
||||
width: item.width,
|
||||
height: item.height,
|
||||
quality: item.quality,
|
||||
entropy: item.entropy,
|
||||
source: item.source,
|
||||
scraper: item.scraper,
|
||||
@@ -317,17 +336,21 @@ function associateTargetMedia(targetId, sources, mediaBySource, domain, role, pr
|
||||
if (!sources) return { [role]: null, [primaryRole]: null };
|
||||
|
||||
const mediaIds = sources
|
||||
.filter(Boolean)
|
||||
.map((source) => {
|
||||
if (!source) return null;
|
||||
|
||||
const mediaItem = Array.isArray(source)
|
||||
? source.reduce((acc, sourceX) => acc || mediaBySource[sourceX.src || sourceX], null)
|
||||
: mediaBySource[source.src || source];
|
||||
|
||||
// return mediaItem && { [`${domain}_id`]: targetId, media_id: mediaItem.id };
|
||||
return mediaItem && mediaItem.id;
|
||||
});
|
||||
return mediaItem;
|
||||
})
|
||||
.filter(Boolean)
|
||||
// .sort((mediaItemA, mediaItemB) => mediaItemB.height - mediaItemA.height) // prefer high res images for primary item
|
||||
.map(mediaItem => mediaItem.id);
|
||||
|
||||
const uniqueMediaIds = Array.from(new Set(mediaIds.filter(Boolean)));
|
||||
const uniqueMediaIds = Array.from(new Set(mediaIds));
|
||||
const associations = uniqueMediaIds.map(mediaId => ({ [`${domain}_id`]: targetId, media_id: mediaId }));
|
||||
|
||||
logger.silly(`Associating ${associations.length} ${role}s to ${domain} ${targetId}`);
|
||||
|
||||
Reference in New Issue
Block a user