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