2019-11-11 02:20:00 +00:00
|
|
|
'use strict';
|
|
|
|
|
2020-03-30 01:01:08 +00:00
|
|
|
const config = require('config');
|
2020-03-29 02:00:46 +00:00
|
|
|
const Promise = require('bluebird');
|
2020-03-31 02:05:31 +00:00
|
|
|
const fs = require('fs').promises;
|
2020-03-30 01:01:08 +00:00
|
|
|
const path = require('path');
|
2020-03-29 02:00:46 +00:00
|
|
|
const nanoid = require('nanoid/non-secure');
|
2020-03-30 01:01:08 +00:00
|
|
|
const mime = require('mime');
|
2020-04-11 20:49:37 +00:00
|
|
|
// const fileType = require('file-type');
|
2020-03-30 01:01:08 +00:00
|
|
|
const sharp = require('sharp');
|
|
|
|
const blake2 = require('blake2');
|
2019-11-11 02:20:00 +00:00
|
|
|
|
2020-03-29 02:00:46 +00:00
|
|
|
const logger = require('./logger')(__filename);
|
|
|
|
const argv = require('./argv');
|
|
|
|
const knex = require('./knex');
|
2020-03-30 01:01:08 +00:00
|
|
|
const http = require('./utils/http');
|
2020-03-29 02:00:46 +00:00
|
|
|
const { get } = require('./utils/qu');
|
|
|
|
|
2020-03-30 01:01:08 +00:00
|
|
|
function getHash(buffer) {
|
|
|
|
const hash = blake2.createHash('blake2b', { digestLength: 24 });
|
|
|
|
hash.update(buffer);
|
|
|
|
|
|
|
|
return hash.digest('hex');
|
|
|
|
}
|
|
|
|
|
2020-03-31 02:05:31 +00:00
|
|
|
async function getEntropy(buffer, source) {
|
2020-03-30 01:01:08 +00:00
|
|
|
try {
|
|
|
|
const { entropy } = await sharp(buffer).stats();
|
|
|
|
|
|
|
|
return entropy;
|
|
|
|
} catch (error) {
|
2020-03-31 02:05:31 +00:00
|
|
|
logger.warn(`Failed to retrieve image entropy, using 7.5 for ${source.src}: ${error.message}`);
|
2020-03-30 01:01:08 +00:00
|
|
|
|
|
|
|
return 7.5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 02:05:31 +00:00
|
|
|
async function getMeta(buffer, source) {
|
2020-03-30 01:01:08 +00:00
|
|
|
try {
|
|
|
|
const { width, height, size } = await sharp(buffer).metadata();
|
|
|
|
|
|
|
|
return {
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
size,
|
|
|
|
};
|
|
|
|
} catch (error) {
|
2020-03-31 02:05:31 +00:00
|
|
|
logger.warn(`Failed to retrieve image metadata from ${source.src}: ${error.message}`);
|
2020-03-30 01:01:08 +00:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getThumbnail(buffer, height = config.media.thumbnailSize) {
|
|
|
|
try {
|
|
|
|
const thumbnail = sharp(buffer)
|
|
|
|
.resize({
|
|
|
|
height,
|
|
|
|
withoutEnlargement: true,
|
|
|
|
})
|
|
|
|
.jpeg({
|
|
|
|
quality: config.media.thumbnailQuality,
|
|
|
|
})
|
|
|
|
.toBuffer();
|
|
|
|
|
|
|
|
return thumbnail;
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(`Failed to create thumbnail: ${error.message}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-04-11 20:49:37 +00:00
|
|
|
function sampleMedias(medias, limit = config.media.limit, preferLast = true) {
|
|
|
|
// limit media sets, use extrax as fallbacks
|
|
|
|
if (medias.length <= limit) {
|
|
|
|
return medias;
|
|
|
|
}
|
|
|
|
|
|
|
|
const chunkSize = Math.floor(medias.length / limit);
|
|
|
|
const rest = medias.length - (limit * chunkSize);
|
|
|
|
|
|
|
|
const chunks = Array.from(
|
|
|
|
{ length: limit },
|
|
|
|
(value, index) => {
|
|
|
|
const start = (chunkSize * index) + Math.min(index, rest);
|
|
|
|
|
|
|
|
return medias.slice(
|
|
|
|
start,
|
|
|
|
start + chunkSize + (index < rest ? 1 : 0),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
// flip last chunk so the very last image (often the best cumshot) is tried first
|
|
|
|
const lastPreferredChunks = preferLast
|
|
|
|
? chunks.slice(0, -1).concat(chunks.slice(-1).reverse())
|
|
|
|
: chunks;
|
|
|
|
|
|
|
|
const groupedMedias = lastPreferredChunks.map((chunk) => {
|
|
|
|
// merge chunked medias into single media with grouped fallback priorities,
|
|
|
|
// so the first sources of each media is preferred over all second sources, etc.
|
|
|
|
const sources = chunk
|
|
|
|
.reduce((accSources, media) => {
|
|
|
|
media.sources.forEach((source, index) => {
|
|
|
|
if (!accSources[index]) {
|
|
|
|
accSources.push([source]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
accSources[index].push(source);
|
|
|
|
});
|
|
|
|
|
|
|
|
return accSources;
|
|
|
|
}, [])
|
|
|
|
.flat();
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: chunk[0].id,
|
|
|
|
role: chunk[0].role,
|
|
|
|
sources,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return groupedMedias;
|
|
|
|
}
|
|
|
|
|
2020-03-29 02:00:46 +00:00
|
|
|
function itemsByKey(items, key) {
|
|
|
|
return items.reduce((acc, item) => ({ ...acc, [item[key]]: item }), {});
|
2019-11-11 02:20:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 02:00:46 +00:00
|
|
|
function toBaseSource(rawSource) {
|
|
|
|
if (rawSource.src || (rawSource.extract && rawSource.url)) {
|
|
|
|
const baseSource = {};
|
|
|
|
|
|
|
|
if (rawSource.src) baseSource.src = rawSource.src;
|
|
|
|
if (rawSource.quality) baseSource.quality = rawSource.quality;
|
|
|
|
if (rawSource.type) baseSource.type = rawSource.type;
|
|
|
|
|
|
|
|
if (rawSource.url) baseSource.url = rawSource.url;
|
|
|
|
if (rawSource.extract) baseSource.extract = rawSource.extract;
|
|
|
|
|
2020-03-30 01:01:08 +00:00
|
|
|
if (rawSource.referer) baseSource.referer = rawSource.referer;
|
|
|
|
if (rawSource.host) baseSource.host = rawSource.host;
|
|
|
|
|
2020-03-31 02:05:31 +00:00
|
|
|
if (rawSource.copyright) baseSource.copyright = rawSource.copyright;
|
|
|
|
if (rawSource.comment) baseSource.comment = rawSource.comment;
|
|
|
|
if (rawSource.group) baseSource.group = rawSource.group;
|
|
|
|
|
2020-03-29 02:00:46 +00:00
|
|
|
return baseSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof rawSource === 'string') {
|
|
|
|
return {
|
|
|
|
src: rawSource,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-03-30 01:01:08 +00:00
|
|
|
function baseSourceToBaseMedia(baseSource, role) {
|
2020-03-29 02:00:46 +00:00
|
|
|
if (Array.isArray(baseSource)) {
|
|
|
|
if (baseSource.length > 0) {
|
|
|
|
return {
|
|
|
|
id: nanoid(),
|
2020-03-30 01:01:08 +00:00
|
|
|
role,
|
2020-03-29 02:00:46 +00:00
|
|
|
sources: baseSource,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (baseSource) {
|
|
|
|
return {
|
|
|
|
id: nanoid(),
|
2020-03-30 01:01:08 +00:00
|
|
|
role,
|
2020-03-29 02:00:46 +00:00
|
|
|
sources: [baseSource],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-03-30 01:01:08 +00:00
|
|
|
function fallbackMediaToBaseMedia(rawMedia, role) {
|
2020-03-29 02:00:46 +00:00
|
|
|
const baseSources = rawMedia
|
|
|
|
.map(source => toBaseSource(source))
|
|
|
|
.filter(Boolean);
|
|
|
|
|
2020-03-30 01:01:08 +00:00
|
|
|
return baseSourceToBaseMedia(baseSources, role);
|
2020-03-29 02:00:46 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 01:01:08 +00:00
|
|
|
function toBaseMedias(rawMedias, role) {
|
2020-03-29 02:00:46 +00:00
|
|
|
if (!rawMedias || rawMedias.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-04-11 20:49:37 +00:00
|
|
|
const baseMedias = rawMedias.map((rawMedia) => {
|
2020-03-29 02:00:46 +00:00
|
|
|
if (!rawMedia) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(rawMedia)) {
|
|
|
|
// fallback sources provided
|
2020-03-30 01:01:08 +00:00
|
|
|
return fallbackMediaToBaseMedia(rawMedia, role);
|
2020-03-29 02:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const baseSource = toBaseSource(rawMedia);
|
|
|
|
|
2020-03-30 01:01:08 +00:00
|
|
|
return baseSourceToBaseMedia(baseSource, role);
|
2020-03-29 02:00:46 +00:00
|
|
|
}).filter(Boolean);
|
2020-04-11 20:49:37 +00:00
|
|
|
|
|
|
|
const sampledBaseMedias = sampleMedias(baseMedias);
|
|
|
|
|
|
|
|
return sampledBaseMedias;
|
2020-03-29 02:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function findSourceDuplicates(baseMedias) {
|
|
|
|
const sourceUrls = baseMedias
|
|
|
|
.map(baseMedia => baseMedia.sources.map(source => source.src))
|
|
|
|
.flat()
|
|
|
|
.filter(Boolean);
|
|
|
|
|
|
|
|
const extractUrls = baseMedias
|
|
|
|
.map(baseMedia => baseMedia.sources.map(source => source.url))
|
|
|
|
.flat()
|
|
|
|
.filter(Boolean);
|
2020-03-24 02:48:24 +00:00
|
|
|
|
2020-03-29 02:00:46 +00:00
|
|
|
const [existingSourceMedia, existingExtractMedia] = await Promise.all([
|
|
|
|
knex('media').whereIn('source', sourceUrls),
|
|
|
|
knex('media').whereIn('source_page', extractUrls),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const existingSourceMediaByUrl = itemsByKey(existingSourceMedia, 'source');
|
|
|
|
const existingExtractMediaByUrl = itemsByKey(existingExtractMedia, 'source_page');
|
|
|
|
|
2020-04-01 23:10:50 +00:00
|
|
|
return [existingSourceMediaByUrl, existingExtractMediaByUrl];
|
2020-03-29 02:00:46 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 01:01:08 +00:00
|
|
|
async function findHashDuplicates(medias) {
|
2020-03-31 02:05:31 +00:00
|
|
|
const hashes = medias.map(media => media.meta?.hash || media.entry?.hash).filter(Boolean);
|
|
|
|
|
|
|
|
const existingHashMediaEntries = await knex('media').whereIn('hash', hashes);
|
|
|
|
const existingHashMediaEntriesByHash = itemsByKey(existingHashMediaEntries, 'hash');
|
|
|
|
|
2020-04-01 23:10:50 +00:00
|
|
|
const uniqueHashMedias = medias.filter(media => !media.entry && !existingHashMediaEntriesByHash[media.meta?.hash]);
|
|
|
|
|
|
|
|
const { selfDuplicateMedias, selfUniqueMediasByHash } = uniqueHashMedias.reduce((acc, media) => {
|
2020-04-08 12:50:43 +00:00
|
|
|
if (!media.meta?.hash) {
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
2020-04-01 23:10:50 +00:00
|
|
|
if (acc.selfUniqueMediasByHash[media.meta.hash]) {
|
|
|
|
acc.selfDuplicateMedias.push({
|
|
|
|
...media,
|
|
|
|
use: acc.selfUniqueMediasByHash[media.meta.hash].id,
|
|
|
|
});
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}
|
2020-03-31 02:05:31 +00:00
|
|
|
|
2020-04-01 23:10:50 +00:00
|
|
|
acc.selfUniqueMediasByHash[media.meta.hash] = media;
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {
|
|
|
|
selfDuplicateMedias: [],
|
|
|
|
selfUniqueMediasByHash: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
const selfUniqueHashMedias = Object.values(selfUniqueMediasByHash);
|
|
|
|
|
|
|
|
const existingHashMedias = medias
|
2020-03-31 02:05:31 +00:00
|
|
|
.filter(media => existingHashMediaEntriesByHash[media.entry?.hash || media.meta?.hash])
|
|
|
|
.map(media => ({
|
|
|
|
...media,
|
|
|
|
entry: existingHashMediaEntriesByHash[media.entry?.hash || media.meta?.hash],
|
2020-04-01 23:10:50 +00:00
|
|
|
}))
|
|
|
|
.concat(selfDuplicateMedias);
|
2020-03-30 01:01:08 +00:00
|
|
|
|
2020-04-01 23:10:50 +00:00
|
|
|
return [selfUniqueHashMedias, existingHashMedias];
|
2020-03-30 01:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function extractSource(baseSource, { existingExtractMediaByUrl }) {
|
|
|
|
if (typeof baseSource.extract !== 'function' || !baseSource.url) {
|
2020-03-29 02:00:46 +00:00
|
|
|
return baseSource;
|
|
|
|
}
|
|
|
|
|
2020-03-30 01:01:08 +00:00
|
|
|
const existingExtractMedia = existingExtractMediaByUrl[baseSource.url];
|
|
|
|
|
|
|
|
if (existingExtractMedia) {
|
|
|
|
// media entry found by extract URL
|
|
|
|
return {
|
|
|
|
...baseSource,
|
|
|
|
entry: existingExtractMedia,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-29 02:00:46 +00:00
|
|
|
const res = await get(baseSource.url);
|
|
|
|
|
2020-03-30 01:01:08 +00:00
|
|
|
if (res.ok) {
|
|
|
|
const src = await baseSource.extract(res.item);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...baseSource,
|
|
|
|
src,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Could not extract source from ${baseSource.url}: ${res.status}`);
|
2020-03-29 02:00:46 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 02:05:31 +00:00
|
|
|
async function saveMedia(media) {
|
|
|
|
const hashDir = media.meta.hash.slice(0, 2);
|
|
|
|
const hashSubDir = media.meta.hash.slice(2, 4);
|
|
|
|
const hashFilename = media.meta.hash.slice(4);
|
|
|
|
|
|
|
|
const filename = media.quality
|
|
|
|
? `${hashFilename}_${media.quality}.${media.meta.extension}`
|
|
|
|
: `${hashFilename}.${media.meta.extension}`;
|
|
|
|
|
|
|
|
const filedir = path.join(media.role, hashDir, hashSubDir);
|
|
|
|
const filepath = path.join(filedir, filename);
|
|
|
|
|
|
|
|
if (media.meta.type === 'image') {
|
|
|
|
const thumbnail = await getThumbnail(media.file.buffer);
|
|
|
|
|
|
|
|
const thumbdir = path.join(media.role, 'thumbs', hashDir, hashSubDir);
|
|
|
|
const thumbpath = path.join(thumbdir, filename);
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
fs.mkdir(path.join(config.media.path, filedir), { recursive: true }),
|
|
|
|
fs.mkdir(path.join(config.media.path, thumbdir), { recursive: true }),
|
|
|
|
]);
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
fs.writeFile(path.join(config.media.path, filepath), media.file.buffer),
|
|
|
|
fs.writeFile(path.join(config.media.path, thumbpath), thumbnail),
|
|
|
|
]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...media,
|
|
|
|
file: {
|
|
|
|
// buffer is no longer needed, discard to free up memory
|
|
|
|
path: filepath,
|
|
|
|
thumbnail: thumbpath,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
await fs.mkdir(path.join(config.media.path, filedir), { recursive: true });
|
|
|
|
await fs.writeFile(path.join(config.media.path, filepath), media.file.buffer);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...media,
|
|
|
|
file: {
|
|
|
|
// buffer is no longer needed, discard to free up memory
|
|
|
|
path: filepath,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchSource(source) {
|
2020-03-30 01:01:08 +00:00
|
|
|
logger.silly(`Fetching media from ${source.src}`);
|
2020-03-29 02:00:46 +00:00
|
|
|
// attempts
|
|
|
|
|
2020-03-30 01:01:08 +00:00
|
|
|
async function attempt(attempts = 1) {
|
|
|
|
try {
|
|
|
|
const { pathname } = new URL(source.src);
|
|
|
|
const mimetype = mime.getType(pathname);
|
|
|
|
const extension = mime.getExtension(mimetype);
|
2020-04-08 12:50:43 +00:00
|
|
|
const type = mimetype?.split('/')[0] || 'image';
|
2020-03-30 01:01:08 +00:00
|
|
|
|
|
|
|
const res = await http.get(source.src, {
|
|
|
|
...(source.referer && { referer: source.referer }),
|
|
|
|
...(source.host && { host: source.host }),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error(`Response ${res.status} not OK`);
|
|
|
|
}
|
|
|
|
|
2020-03-31 02:05:31 +00:00
|
|
|
const hash = getHash(res.body);
|
|
|
|
const entropy = type === 'image' ? await getEntropy(res.body) : null;
|
|
|
|
const { size, width, height } = type === 'image' ? await getMeta(res.body) : {};
|
2020-03-30 01:01:08 +00:00
|
|
|
|
|
|
|
logger.silly(`Fetched media from ${source.src}`);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...source,
|
|
|
|
file: {
|
2020-03-31 02:05:31 +00:00
|
|
|
buffer: res.body,
|
|
|
|
},
|
|
|
|
meta: {
|
2020-03-30 01:01:08 +00:00
|
|
|
mimetype,
|
|
|
|
extension,
|
2020-03-31 02:05:31 +00:00
|
|
|
type,
|
2020-03-30 01:01:08 +00:00
|
|
|
hash,
|
|
|
|
entropy,
|
|
|
|
size,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
logger.warn(`Failed attempt ${attempts}/3 to fetch ${source.src}: ${error.message}`);
|
|
|
|
|
|
|
|
if (attempts < 3) {
|
|
|
|
await Promise.delay(1000);
|
|
|
|
return attempt(attempts + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Failed to fetch ${source.src}: ${error.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return attempt(1);
|
|
|
|
}
|
|
|
|
|
2020-04-01 23:10:50 +00:00
|
|
|
async function trySource(baseSource, existingMedias) {
|
2020-03-30 01:01:08 +00:00
|
|
|
// catch error and try the next source
|
|
|
|
const extractedSource = await extractSource(baseSource, existingMedias);
|
|
|
|
const existingSourceMedia = existingMedias.existingSourceMediaByUrl[extractedSource.src];
|
|
|
|
|
|
|
|
if (extractedSource.entry) {
|
2020-03-31 02:05:31 +00:00
|
|
|
logger.silly(`Media page URL already in database, not extracting ${baseSource.url}`);
|
|
|
|
|
2020-03-30 01:01:08 +00:00
|
|
|
// media entry found during extraction, don't fetch
|
|
|
|
return extractedSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (existingSourceMedia) {
|
2020-04-01 23:10:50 +00:00
|
|
|
logger.silly(`Media source URL already in database, skipping ${baseSource.src}`);
|
2020-03-31 02:05:31 +00:00
|
|
|
|
2020-03-30 01:01:08 +00:00
|
|
|
// media entry found by source URL, don't fetch
|
|
|
|
return {
|
|
|
|
...baseSource,
|
|
|
|
entry: existingSourceMedia,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-01 23:10:50 +00:00
|
|
|
return fetchSource(extractedSource);
|
2020-03-29 02:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchMedia(baseMedia, existingMedias) {
|
2020-03-30 01:01:08 +00:00
|
|
|
try {
|
|
|
|
const source = await baseMedia.sources.reduce(
|
|
|
|
// try each source until success
|
|
|
|
(result, baseSource, baseSourceIndex) => result.catch(async () => trySource(baseSource, existingMedias, baseMedia, baseSourceIndex)),
|
|
|
|
Promise.reject(new Error()),
|
|
|
|
);
|
|
|
|
|
2020-03-31 02:05:31 +00:00
|
|
|
if (source.entry) {
|
|
|
|
// don't save media, already in database
|
|
|
|
return {
|
|
|
|
...baseMedia,
|
|
|
|
...source,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return saveMedia({
|
2020-03-30 01:01:08 +00:00
|
|
|
...baseMedia,
|
|
|
|
...source,
|
2020-03-31 02:05:31 +00:00
|
|
|
});
|
2020-03-30 01:01:08 +00:00
|
|
|
} catch (error) {
|
|
|
|
logger.warn(error.message);
|
|
|
|
|
|
|
|
return baseMedia;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 02:05:31 +00:00
|
|
|
function curateMediaEntry(media, index) {
|
|
|
|
if (media.entry) {
|
|
|
|
return media;
|
2020-03-30 01:01:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 02:05:31 +00:00
|
|
|
const curatedMediaEntry = {
|
|
|
|
id: media.id,
|
|
|
|
path: media.file.path,
|
|
|
|
thumbnail: media.file.thumbnail,
|
|
|
|
index,
|
|
|
|
mime: media.meta.mimetype,
|
|
|
|
hash: media.meta.hash,
|
|
|
|
size: media.meta.size,
|
|
|
|
width: media.meta.width,
|
|
|
|
height: media.meta.height,
|
|
|
|
entropy: media.meta.entropy,
|
|
|
|
source: media.src,
|
|
|
|
source_page: media.url,
|
|
|
|
scraper: media.scraper,
|
|
|
|
copyright: media.copyright,
|
|
|
|
comment: media.comment,
|
|
|
|
};
|
2020-03-30 01:01:08 +00:00
|
|
|
|
2020-03-31 02:05:31 +00:00
|
|
|
return {
|
|
|
|
...media,
|
|
|
|
newEntry: true,
|
|
|
|
entry: curatedMediaEntry,
|
|
|
|
};
|
|
|
|
}
|
2020-03-30 01:01:08 +00:00
|
|
|
|
2020-03-31 02:05:31 +00:00
|
|
|
async function storeMedias(baseMedias) {
|
2020-04-01 23:10:50 +00:00
|
|
|
const [existingSourceMediaByUrl, existingExtractMediaByUrl] = await findSourceDuplicates(baseMedias);
|
2020-03-30 01:01:08 +00:00
|
|
|
|
2020-03-31 02:05:31 +00:00
|
|
|
const savedMedias = await Promise.map(
|
|
|
|
baseMedias,
|
|
|
|
async baseMedia => fetchMedia(baseMedia, { existingSourceMediaByUrl, existingExtractMediaByUrl }),
|
2020-04-16 20:23:25 +00:00
|
|
|
{ concurrency: 10 },
|
2020-03-31 02:05:31 +00:00
|
|
|
);
|
2020-03-30 01:01:08 +00:00
|
|
|
|
2020-04-01 23:10:50 +00:00
|
|
|
const [uniqueHashMedias, existingHashMedias] = await findHashDuplicates(savedMedias);
|
2020-03-29 02:00:46 +00:00
|
|
|
|
2020-04-01 23:10:50 +00:00
|
|
|
const newMediaWithEntries = uniqueHashMedias.map((media, index) => curateMediaEntry(media, index));
|
|
|
|
const newMediaEntries = newMediaWithEntries.filter(media => media.newEntry).map(media => media.entry);
|
2020-03-29 02:00:46 +00:00
|
|
|
|
2020-03-31 02:05:31 +00:00
|
|
|
await knex('media').insert(newMediaEntries);
|
2020-03-29 02:00:46 +00:00
|
|
|
|
2020-04-01 23:10:50 +00:00
|
|
|
return [...newMediaWithEntries, ...existingHashMedias];
|
2020-03-29 02:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function associateReleaseMedia(releases) {
|
|
|
|
if (!argv.media) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const baseMediasByReleaseId = releases.reduce((acc, release) => ({
|
|
|
|
...acc,
|
2020-03-31 02:05:31 +00:00
|
|
|
[release.id]: [
|
|
|
|
...(argv.images && argv.poster ? toBaseMedias([release.poster], 'posters') : []),
|
2020-04-16 20:54:07 +00:00
|
|
|
...(argv.images && argv.poster ? toBaseMedias(release.covers, 'covers') : []),
|
2020-04-01 23:10:50 +00:00
|
|
|
...(argv.images && argv.photos ? toBaseMedias(release.photos, 'photos') : []),
|
2020-03-31 02:05:31 +00:00
|
|
|
...(argv.videos && argv.trailer ? toBaseMedias([release.trailer], 'trailers') : []),
|
|
|
|
...(argv.videos && argv.teaser ? toBaseMedias([release.teaser], 'teasers') : []),
|
|
|
|
],
|
2020-03-29 02:00:46 +00:00
|
|
|
}), {});
|
|
|
|
|
2020-04-16 14:24:59 +00:00
|
|
|
const baseMediasByRole = Object.values(baseMediasByReleaseId)
|
|
|
|
.flat()
|
|
|
|
.filter(Boolean)
|
|
|
|
.reduce((acc, baseMedia) => {
|
|
|
|
if (!acc[baseMedia.role]) acc[baseMedia.role] = [];
|
|
|
|
acc[baseMedia.role].push(baseMedia);
|
2020-03-29 02:00:46 +00:00
|
|
|
|
2020-04-16 14:24:59 +00:00
|
|
|
return acc;
|
|
|
|
}, {});
|
2020-03-31 02:05:31 +00:00
|
|
|
|
2020-04-16 14:24:59 +00:00
|
|
|
await Promise.reduce(['posters', 'covers', 'photos', 'teasers', 'trailers'], async (chain, role) => {
|
|
|
|
// stage by role so posters are prioritized over photos and videos
|
|
|
|
await chain;
|
2020-03-31 02:05:31 +00:00
|
|
|
|
2020-04-16 14:24:59 +00:00
|
|
|
const baseMedias = baseMediasByRole[role];
|
2020-03-31 02:05:31 +00:00
|
|
|
|
2020-04-16 14:24:59 +00:00
|
|
|
if (!baseMedias) {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-31 02:05:31 +00:00
|
|
|
|
2020-04-16 14:24:59 +00:00
|
|
|
const storedMedias = await storeMedias(baseMedias);
|
|
|
|
const storedMediasById = itemsByKey(storedMedias, 'id');
|
|
|
|
|
|
|
|
const associations = Object
|
|
|
|
.entries(baseMediasByReleaseId)
|
|
|
|
.reduce((acc, [releaseId, releaseBaseMedias]) => {
|
|
|
|
releaseBaseMedias.forEach((baseMedia) => {
|
|
|
|
const media = storedMediasById[baseMedia.id];
|
|
|
|
|
|
|
|
if (media) {
|
|
|
|
acc.push({
|
|
|
|
release_id: releaseId,
|
|
|
|
media_id: media.use || media.entry.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, [])
|
|
|
|
.filter(Boolean);
|
2020-03-31 02:05:31 +00:00
|
|
|
|
2020-04-16 14:24:59 +00:00
|
|
|
await knex.raw(`${knex(`releases_${role}`).insert(associations)} ON CONFLICT DO NOTHING`);
|
|
|
|
}, Promise.resolve());
|
2020-03-29 02:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
associateReleaseMedia,
|
2019-11-11 02:20:00 +00:00
|
|
|
};
|