Compare commits
2 Commits
6c694d7d48
...
c0112d61fb
| Author | SHA1 | Date | |
|---|---|---|---|
| c0112d61fb | |||
| 6b80aee80a |
@ -165,7 +165,14 @@ const buildNeighborsQueryFilter = (filter = {}, search = '') => {
|
||||
return queryFilter;
|
||||
};
|
||||
|
||||
const getNeighborsCacheKey = ({ model, id, filter = {}, search = '', sort = '', order = 'ascend' }) => {
|
||||
const getNeighborsCacheKey = ({
|
||||
model,
|
||||
id,
|
||||
filter = {},
|
||||
search = '',
|
||||
sort = '',
|
||||
order = 'ascend',
|
||||
}) => {
|
||||
const normalizedSort = sort || 'createdAt';
|
||||
const normalizedOrder = order || 'ascend';
|
||||
const queryHash = crypto
|
||||
@ -1186,11 +1193,7 @@ export async function getThumbnail({ fileId, size, file }) {
|
||||
const body = await downloadFile(BUCKETS.FILES, s3Key);
|
||||
const buffer = await streamToBuffer(body);
|
||||
|
||||
await redisServer.setKey(
|
||||
cacheKey,
|
||||
buffer.toString('base64'),
|
||||
FILE_THUMBNAIL_CACHE_TTL_SECONDS
|
||||
);
|
||||
await redisServer.setKey(cacheKey, buffer.toString('base64'), FILE_THUMBNAIL_CACHE_TTL_SECONDS);
|
||||
|
||||
return {
|
||||
buffer,
|
||||
@ -1258,19 +1261,39 @@ async function deleteFileThumbnails(fileObject) {
|
||||
|
||||
export async function createFileThumbnails(fileData, fileObject) {
|
||||
try {
|
||||
if (!fileObject?.type?.startsWith('image/') || !fileData || !fileObject?._id) {
|
||||
const extension = fileObject?.extension?.toLowerCase();
|
||||
const isGCodeFileType = extension === '.gcode' || extension === '.g';
|
||||
const gcodeThumbnail = fileObject?.metaData?.thumbnail;
|
||||
const hasGCodeThumbnailMeta =
|
||||
isGCodeFileType && gcodeThumbnail && typeof gcodeThumbnail.data === 'string';
|
||||
const isImageFileType = fileObject?.type?.startsWith('image/');
|
||||
|
||||
const hasThumbnailSource =
|
||||
(hasGCodeThumbnailMeta && gcodeThumbnail.data.length > 0) ||
|
||||
(isImageFileType && fileData);
|
||||
|
||||
if (!hasThumbnailSource || !fileObject?._id) {
|
||||
return;
|
||||
}
|
||||
|
||||
let thumbnailData = fileData;
|
||||
if (hasGCodeThumbnailMeta) {
|
||||
const normalized = gcodeThumbnail.data
|
||||
.replace(/^data:image\/\w+;base64,/, '')
|
||||
.replace(/\s/g, '');
|
||||
thumbnailData = Buffer.from(normalized, 'base64');
|
||||
if (!thumbnailData.length) return;
|
||||
}
|
||||
|
||||
const fileId = fileObject._id.toString();
|
||||
const image = sharp(fileData, { failOn: 'none' });
|
||||
const image = sharp(thumbnailData, { failOn: 'none' });
|
||||
const imageMetadata = await image.metadata();
|
||||
|
||||
if (!imageMetadata.width || !imageMetadata.height) {
|
||||
return;
|
||||
}
|
||||
|
||||
const blurhashSource = await sharp(fileData)
|
||||
const blurhashSource = await sharp(thumbnailData)
|
||||
.rotate()
|
||||
.resize(32, 32, { fit: 'inside' })
|
||||
.ensureAlpha()
|
||||
@ -1288,7 +1311,7 @@ export async function createFileThumbnails(fileData, fileObject) {
|
||||
const thumbnails = {};
|
||||
|
||||
for (const size of FILE_THUMBNAIL_SIZES) {
|
||||
const thumbnailBuffer = await sharp(fileData)
|
||||
const thumbnailBuffer = await sharp(thumbnailData)
|
||||
.rotate()
|
||||
.resize(size, size, { fit: 'inside', withoutEnlargement: true })
|
||||
.jpeg({ quality: 85 })
|
||||
@ -1317,6 +1340,7 @@ export async function createFileThumbnails(fileData, fileObject) {
|
||||
metaData: {
|
||||
...(fileObject.metaData || {}),
|
||||
blurHash,
|
||||
thumbnails,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@ -145,6 +145,8 @@ export const slicerUploadRouteHandler = async (req, res) => {
|
||||
return res.status(createdFile.code || 500).send(createdFile);
|
||||
}
|
||||
|
||||
createFileThumbnails(file.buffer, createdFile);
|
||||
|
||||
cephKey = `files/${createdFile._id}${extension}`;
|
||||
await uploadFile(BUCKETS.FILES, cephKey, file.buffer, contentType, {
|
||||
originalName,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user