Added various tag photos. Renamed some toy tags.
This commit is contained in:
158
src/media.js
158
src/media.js
@@ -200,6 +200,8 @@ async function findSourceDuplicates(baseMedias) {
|
||||
knex('media').whereIn('source_page', extractUrls),
|
||||
]);
|
||||
|
||||
console.log(sourceUrls, existingSourceMedia);
|
||||
|
||||
const existingSourceMediaByUrl = itemsByKey(existingSourceMedia, 'source');
|
||||
const existingExtractMediaByUrl = itemsByKey(existingExtractMedia, 'source_page');
|
||||
|
||||
@@ -279,86 +281,94 @@ async function extractSource(baseSource, { existingExtractMediaByUrl }) {
|
||||
}
|
||||
|
||||
async function storeImageFile(media, hashDir, hashSubDir, filename, filedir, filepath) {
|
||||
const thumbdir = path.join(media.role, 'thumbs', hashDir, hashSubDir);
|
||||
const thumbpath = path.join(thumbdir, filename);
|
||||
try {
|
||||
const thumbdir = path.join(media.role, 'thumbs', hashDir, hashSubDir);
|
||||
const thumbpath = path.join(thumbdir, filename);
|
||||
|
||||
const lazydir = path.join(media.role, 'lazy', hashDir, hashSubDir);
|
||||
const lazypath = path.join(lazydir, filename);
|
||||
const lazydir = path.join(media.role, 'lazy', hashDir, hashSubDir);
|
||||
const lazypath = path.join(lazydir, filename);
|
||||
|
||||
await Promise.all([
|
||||
fsPromises.mkdir(path.join(config.media.path, filedir), { recursive: true }),
|
||||
fsPromises.mkdir(path.join(config.media.path, thumbdir), { recursive: true }),
|
||||
fsPromises.mkdir(path.join(config.media.path, lazydir), { recursive: true }),
|
||||
]);
|
||||
await Promise.all([
|
||||
fsPromises.mkdir(path.join(config.media.path, filedir), { recursive: true }),
|
||||
fsPromises.mkdir(path.join(config.media.path, thumbdir), { recursive: true }),
|
||||
fsPromises.mkdir(path.join(config.media.path, lazydir), { recursive: true }),
|
||||
]);
|
||||
|
||||
const image = sharp(media.file.path);
|
||||
const info = await image.metadata();
|
||||
const isProcessed = media.meta.subtype !== 'jpeg' || media.process;
|
||||
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 (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;
|
||||
}
|
||||
if (operation === 'crop') {
|
||||
image.extract(...(Array.isArray(options) ? options : [options]));
|
||||
return;
|
||||
}
|
||||
|
||||
logger.warn(`Unknown image operation on ${media.id} (${media.src}): ${operation}`);
|
||||
});
|
||||
}
|
||||
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));
|
||||
}
|
||||
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([
|
||||
image
|
||||
.resize({
|
||||
height: config.media.thumbnailSize,
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.jpeg({ quality: config.media.thumbnailQuality })
|
||||
.toFile(path.join(config.media.path, thumbpath)),
|
||||
image
|
||||
.resize({
|
||||
height: config.media.lazySize,
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.jpeg({ quality: config.media.lazyQuality })
|
||||
.toFile(path.join(config.media.path, lazypath)),
|
||||
]);
|
||||
// generate thumbnail and lazy
|
||||
await Promise.all([
|
||||
image
|
||||
.resize({
|
||||
height: config.media.thumbnailSize,
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.jpeg({ quality: config.media.thumbnailQuality })
|
||||
.toFile(path.join(config.media.path, thumbpath)),
|
||||
image
|
||||
.resize({
|
||||
height: config.media.lazySize,
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.jpeg({ quality: config.media.lazyQuality })
|
||||
.toFile(path.join(config.media.path, lazypath)),
|
||||
]);
|
||||
|
||||
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}`);
|
||||
|
||||
return {
|
||||
...media,
|
||||
file: {
|
||||
path: filepath,
|
||||
thumbnail: thumbpath,
|
||||
lazy: lazypath,
|
||||
},
|
||||
meta: {
|
||||
...media.meta,
|
||||
width: info.width,
|
||||
height: info.height,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error(`Failed to store ${media.id} from ${media.src} at ${filepath}: ${error.message}`);
|
||||
|
||||
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));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
logger.silly(`Stored thumbnail, lazy and permanent media file for ${media.id} from ${media.src} at ${filepath}`);
|
||||
|
||||
return {
|
||||
...media,
|
||||
file: {
|
||||
path: filepath,
|
||||
thumbnail: thumbpath,
|
||||
lazy: lazypath,
|
||||
},
|
||||
meta: {
|
||||
...media.meta,
|
||||
width: info.width,
|
||||
height: info.height,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function storeFile(media) {
|
||||
@@ -374,6 +384,15 @@ async function storeFile(media) {
|
||||
const filedir = path.join(media.role, hashDir, hashSubDir);
|
||||
const filepath = path.join(filedir, filename);
|
||||
|
||||
if (argv.force) {
|
||||
try {
|
||||
// remove old file to in case rename() does not overwrite (possibly on NFS setups)
|
||||
await fsPromises.unlink(path.join(config.media.path, filepath));
|
||||
} catch (error) {
|
||||
// file probably didn't exist
|
||||
}
|
||||
}
|
||||
|
||||
if (media.meta.type === 'image') {
|
||||
return storeImageFile(media, hashDir, hashSubDir, filename, filedir, filepath);
|
||||
}
|
||||
@@ -383,6 +402,7 @@ async function storeFile(media) {
|
||||
fsPromises.mkdir(path.join(config.media.path, filedir), { recursive: true }),
|
||||
]);
|
||||
|
||||
// move temp file to permanent location
|
||||
await fsPromises.rename(media.file.path, path.join(config.media.path, filepath));
|
||||
|
||||
logger.silly(`Stored permanent media file for ${media.id} from ${media.src} at ${filepath}`);
|
||||
|
||||
Reference in New Issue
Block a user