Added image processing options to media module. Cropping Killergram avatars. Overwriting images when --force is used.

This commit is contained in:
ThePendulum 2020-07-10 03:42:08 +02:00
parent 4c551cc15f
commit e8c55512e2
2 changed files with 44 additions and 12 deletions

View File

@ -105,6 +105,8 @@ function toBaseSource(rawSource) {
if (rawSource.comment) baseSource.comment = rawSource.comment;
if (rawSource.group) baseSource.group = rawSource.group;
if (rawSource.process) baseSource.process = rawSource.process;
return baseSource;
}
@ -285,6 +287,30 @@ async function storeImageFile(media, hashDir, hashSubDir, filename, filedir, fil
const image = sharp(media.file.path);
const info = await image.metadata();
const isProcessed = media.meta.subtype !== 'jpeg' || media.process;
if (media.process) {
Object.entries(media.process).forEach(([operation, options]) => {
if (image[operation]) {
image[operation](...(Array.isArray(options) ? options : [options]));
return;
}
if (operation === 'crop') {
image.extract(...(Array.isArray(options) ? options : [options]));
return;
}
logger.warn(`Unknown image operation on ${media.id} (${media.src}): ${operation}`);
});
}
if (isProcessed) {
// convert to JPEG and write to permanent location
await image
.jpeg()
.toFile(path.join(config.media.path, filepath));
}
// generate thumbnail and lazy
await Promise.all([
@ -304,17 +330,12 @@ async function storeImageFile(media, hashDir, hashSubDir, filename, filedir, fil
.toFile(path.join(config.media.path, lazypath)),
]);
if (media.meta.subtype === 'jpeg') {
// move temp file to permanent location
await fsPromises.rename(media.file.path, path.join(config.media.path, filepath));
} else {
// convert to JPEG and write to permanent location
await sharp(media.file.path)
.jpeg()
.toFile(path.join(config.media.path, filepath));
if (isProcessed) {
// remove temp file
await fsPromises.unlink(media.file.path);
} else {
// move temp file to permanent location
await fsPromises.rename(media.file.path, path.join(config.media.path, filepath));
}
logger.silly(`Stored thumbnail, lazy and permanent media file for ${media.id} from ${media.src} at ${filepath}`);
@ -435,8 +456,6 @@ async function fetchSource(source, baseMedia) {
hasher.write(chunk);
});
console.log(source);
const { mimetype } = source.stream
? await fetchStreamSource(source, tempFileTarget, hashStream)
: await fetchHttpSource(source, tempFileTarget, hashStream);
@ -572,9 +591,10 @@ async function storeMedias(baseMedias) {
);
const [uniqueHashMedias, existingHashMedias] = await findHashDuplicates(fetchedMedias);
const newMedias = argv.force ? uniqueHashMedias.concat(existingHashMedias) : uniqueHashMedias;
const savedMedias = await Promise.map(
uniqueHashMedias,
newMedias,
async baseMedia => storeFile(baseMedia),
);

View File

@ -71,6 +71,18 @@ async function fetchActorReleases({ query }, url, remainingPages, actorName, acc
async function scrapeProfile({ query, window }, actorName, url, include) {
const profile = {};
profile.avatar = {
src: `http://thumbs.killergram.com/models/${encodeURI(actorName)}/modelprofilethumb.jpg`,
process: {
crop: {
top: 4,
left: 4,
width: 289,
height: 125,
},
},
};
if (include.releases) {
const availablePages = query.all('.pageboxdropdown option', 'value');
profile.releases = await fetchActorReleases(qu.init(query.q('#episodes > table'), window), url, availablePages.slice(1), actorName);