Added image processing options to media module. Cropping Killergram avatars. Overwriting images when --force is used.
This commit is contained in:
parent
4c551cc15f
commit
e8c55512e2
44
src/media.js
44
src/media.js
|
@ -105,6 +105,8 @@ function toBaseSource(rawSource) {
|
||||||
if (rawSource.comment) baseSource.comment = rawSource.comment;
|
if (rawSource.comment) baseSource.comment = rawSource.comment;
|
||||||
if (rawSource.group) baseSource.group = rawSource.group;
|
if (rawSource.group) baseSource.group = rawSource.group;
|
||||||
|
|
||||||
|
if (rawSource.process) baseSource.process = rawSource.process;
|
||||||
|
|
||||||
return baseSource;
|
return baseSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -285,6 +287,30 @@ async function storeImageFile(media, hashDir, hashSubDir, filename, filedir, fil
|
||||||
|
|
||||||
const image = sharp(media.file.path);
|
const image = sharp(media.file.path);
|
||||||
const info = await image.metadata();
|
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
|
// generate thumbnail and lazy
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
|
@ -304,17 +330,12 @@ async function storeImageFile(media, hashDir, hashSubDir, filename, filedir, fil
|
||||||
.toFile(path.join(config.media.path, lazypath)),
|
.toFile(path.join(config.media.path, lazypath)),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (media.meta.subtype === 'jpeg') {
|
if (isProcessed) {
|
||||||
// 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));
|
|
||||||
|
|
||||||
// remove temp file
|
// remove temp file
|
||||||
await fsPromises.unlink(media.file.path);
|
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}`);
|
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);
|
hasher.write(chunk);
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(source);
|
|
||||||
|
|
||||||
const { mimetype } = source.stream
|
const { mimetype } = source.stream
|
||||||
? await fetchStreamSource(source, tempFileTarget, hashStream)
|
? await fetchStreamSource(source, tempFileTarget, hashStream)
|
||||||
: await fetchHttpSource(source, tempFileTarget, hashStream);
|
: await fetchHttpSource(source, tempFileTarget, hashStream);
|
||||||
|
@ -572,9 +591,10 @@ async function storeMedias(baseMedias) {
|
||||||
);
|
);
|
||||||
|
|
||||||
const [uniqueHashMedias, existingHashMedias] = await findHashDuplicates(fetchedMedias);
|
const [uniqueHashMedias, existingHashMedias] = await findHashDuplicates(fetchedMedias);
|
||||||
|
const newMedias = argv.force ? uniqueHashMedias.concat(existingHashMedias) : uniqueHashMedias;
|
||||||
|
|
||||||
const savedMedias = await Promise.map(
|
const savedMedias = await Promise.map(
|
||||||
uniqueHashMedias,
|
newMedias,
|
||||||
async baseMedia => storeFile(baseMedia),
|
async baseMedia => storeFile(baseMedia),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -71,6 +71,18 @@ async function fetchActorReleases({ query }, url, remainingPages, actorName, acc
|
||||||
async function scrapeProfile({ query, window }, actorName, url, include) {
|
async function scrapeProfile({ query, window }, actorName, url, include) {
|
||||||
const profile = {};
|
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) {
|
if (include.releases) {
|
||||||
const availablePages = query.all('.pageboxdropdown option', 'value');
|
const availablePages = query.all('.pageboxdropdown option', 'value');
|
||||||
profile.releases = await fetchActorReleases(qu.init(query.q('#episodes > table'), window), url, availablePages.slice(1), actorName);
|
profile.releases = await fetchActorReleases(qu.init(query.q('#episodes > table'), window), url, availablePages.slice(1), actorName);
|
||||||
|
|
Loading…
Reference in New Issue