u
This commit is contained in:
@@ -9,6 +9,7 @@ import categoryGetTreeForCompany from './store/category/getTreeForCompany.js';
|
||||
import categoryGetCategoryArticles from './store/category/getCategoryArticles.js';
|
||||
import categoryCreateUnderParent from './store/category/createUnderParent.js';
|
||||
import categoryDelete from './store/category/delete.js';
|
||||
import articleAddImage from './store/article/addImage.js';
|
||||
import articleCreateUnderParent from './store/article/createUnderParent.js';
|
||||
import articleGetBySku from './store/article/getBySku.js';
|
||||
import articleUpdateBySku from './store/article/updateBySku.js';
|
||||
@@ -57,6 +58,7 @@ export default class LibDashStore {
|
||||
createUnderParent: articleCreateUnderParent(pool),
|
||||
getBySku: articleGetBySku(pool),
|
||||
updateBySku: articleUpdateBySku(pool),
|
||||
addImage: articleAddImage(pool),
|
||||
};
|
||||
|
||||
this.lang = {
|
||||
|
||||
@@ -90,7 +90,9 @@ BEGIN
|
||||
thumbw INT NOT NULL,
|
||||
thumbh INT NOT NULL,
|
||||
thumbhash NVARCHAR(64) NOT NULL,
|
||||
source NVARCHAR(MAX) NOT NULL
|
||||
source NVARCHAR(MAX) NOT NULL,
|
||||
[binary] VARBINARY(MAX) NOT NULL,
|
||||
thumbbinary VARBINARY(MAX) NOT NULL
|
||||
);
|
||||
END
|
||||
`;
|
||||
|
||||
10
src/sql/articlepic.js
Normal file
10
src/sql/articlepic.js
Normal file
@@ -0,0 +1,10 @@
|
||||
export const GET_NEXT_SORT = `
|
||||
SELECT ISNULL(MAX(sort), -1) + 1 AS sort
|
||||
FROM articlepic
|
||||
WHERE article_id = @articleId
|
||||
`;
|
||||
|
||||
export const ADD = `
|
||||
INSERT INTO articlepic (article_id, pic_id, sort)
|
||||
VALUES (@articleId, @picId, @sort)
|
||||
`;
|
||||
9
src/sql/pic.js
Normal file
9
src/sql/pic.js
Normal file
@@ -0,0 +1,9 @@
|
||||
export const CREATE = `
|
||||
INSERT INTO pic (
|
||||
w, h, size, hash, thumbw, thumbh, thumbhash, source, [binary], thumbbinary
|
||||
)
|
||||
OUTPUT INSERTED.id
|
||||
VALUES (
|
||||
@w, @h, @size, @hash, @thumbw, @thumbh, @thumbhash, @source, @binary, @thumbbinary
|
||||
)
|
||||
`;
|
||||
83
src/store/article/addImage.js
Normal file
83
src/store/article/addImage.js
Normal file
@@ -0,0 +1,83 @@
|
||||
import { GET_CATEGORY_ID_BY_SKU } from '../../sql/article.js';
|
||||
import { ADD as ADD_ARTICLEPIC, GET_NEXT_SORT } from '../../sql/articlepic.js';
|
||||
import { CREATE as CREATE_PIC } from '../../sql/pic.js';
|
||||
import processAvifImage from '../../util/processAvifImage.js';
|
||||
import resolveCategoryAccess from '../category/resolveCategoryAccess.js';
|
||||
import request from '../request.js';
|
||||
|
||||
export default function addImage(pool) {
|
||||
const access = resolveCategoryAccess(pool);
|
||||
|
||||
return async (googleAuthId, sku, image) => {
|
||||
const categoryLookup = await pool
|
||||
.request()
|
||||
.input('sku', sku)
|
||||
.query(GET_CATEGORY_ID_BY_SKU);
|
||||
|
||||
const row = categoryLookup.recordset[0];
|
||||
if (!row?.categoryId || !row?.articleId) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
await access(googleAuthId, row.categoryId);
|
||||
|
||||
const processed = await processAvifImage(image);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
const sortResult = await request(pool, tx)
|
||||
.input('articleId', row.articleId)
|
||||
.query(GET_NEXT_SORT);
|
||||
|
||||
const sort = sortResult.recordset[0].sort;
|
||||
|
||||
const picResult = await request(pool, tx)
|
||||
.input('w', processed.full.w)
|
||||
.input('h', processed.full.h)
|
||||
.input('size', processed.full.size)
|
||||
.input('hash', processed.full.hash)
|
||||
.input('thumbw', processed.thumb.w)
|
||||
.input('thumbh', processed.thumb.h)
|
||||
.input('thumbhash', processed.thumb.hash)
|
||||
.input('source', '')
|
||||
.input('binary', processed.full.buffer)
|
||||
.input('thumbbinary', processed.thumb.buffer)
|
||||
.query(CREATE_PIC);
|
||||
|
||||
const picId = picResult.recordset[0].id;
|
||||
|
||||
await request(pool, tx)
|
||||
.input('articleId', row.articleId)
|
||||
.input('picId', picId)
|
||||
.input('sort', sort)
|
||||
.query(ADD_ARTICLEPIC);
|
||||
|
||||
await tx.commit();
|
||||
|
||||
return {
|
||||
picId,
|
||||
sort,
|
||||
full: {
|
||||
w: processed.full.w,
|
||||
h: processed.full.h,
|
||||
size: processed.full.size,
|
||||
},
|
||||
thumb: {
|
||||
w: processed.thumb.w,
|
||||
h: processed.thumb.h,
|
||||
},
|
||||
hash: processed.full.hash,
|
||||
thumbHash: processed.thumb.hash,
|
||||
};
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
}
|
||||
51
src/util/processAvifImage.js
Normal file
51
src/util/processAvifImage.js
Normal file
@@ -0,0 +1,51 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import sharp from 'sharp';
|
||||
import { rgbaToThumbHash } from 'thumbhash';
|
||||
|
||||
const FULL_MAX = 1000;
|
||||
const THUMB_MAX = 200;
|
||||
|
||||
export default async function processAvifImage(inputBuffer) {
|
||||
const input = Buffer.isBuffer(inputBuffer)
|
||||
? inputBuffer
|
||||
: Buffer.from(inputBuffer);
|
||||
|
||||
const [full, thumb, thumbRgba] = await Promise.all([
|
||||
sharp(input)
|
||||
.resize(FULL_MAX, FULL_MAX, { fit: 'inside', withoutEnlargement: true })
|
||||
.avif({ effort: 4 })
|
||||
.toBuffer({ resolveWithObject: true }),
|
||||
sharp(input)
|
||||
.resize(THUMB_MAX, THUMB_MAX, { fit: 'inside', withoutEnlargement: true })
|
||||
.avif({ effort: 4 })
|
||||
.toBuffer({ resolveWithObject: true }),
|
||||
sharp(input)
|
||||
.resize(100, 100, { fit: 'inside', withoutEnlargement: true })
|
||||
.ensureAlpha()
|
||||
.raw()
|
||||
.toBuffer({ resolveWithObject: true }),
|
||||
]);
|
||||
|
||||
const thumbHashBytes = rgbaToThumbHash(
|
||||
thumbRgba.info.width,
|
||||
thumbRgba.info.height,
|
||||
thumbRgba.data,
|
||||
);
|
||||
|
||||
return {
|
||||
full: {
|
||||
w: full.info.width,
|
||||
h: full.info.height,
|
||||
size: full.data.length,
|
||||
buffer: full.data,
|
||||
hash: createHash('sha256').update(full.data).digest('hex'),
|
||||
},
|
||||
thumb: {
|
||||
w: thumb.info.width,
|
||||
h: thumb.info.height,
|
||||
size: thumb.data.length,
|
||||
buffer: thumb.data,
|
||||
hash: Buffer.from(thumbHashBytes).toString('base64'),
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user