sqlite
This commit is contained in:
@@ -1,12 +1,25 @@
|
||||
import { ADD as ADD_ARTICLEPIC, GET_NEXT_SORT } from '../../providers/mssql/queries/articlepic.js';
|
||||
import { CREATE as CREATE_PIC } from '../../providers/mssql/queries/pic.js';
|
||||
import {
|
||||
ADD as MSSQL_ADD_ARTICLEPIC,
|
||||
GET_NEXT_SORT as MSSQL_GET_NEXT_SORT,
|
||||
} from '../../providers/mssql/queries/articlepic.js';
|
||||
import {
|
||||
ADD as SQLITE_ADD_ARTICLEPIC,
|
||||
GET_NEXT_SORT as SQLITE_GET_NEXT_SORT,
|
||||
} from '../../providers/sqlite3/queries/articlepic.js';
|
||||
import { CREATE as MSSQL_CREATE_PIC } from '../../providers/mssql/queries/pic.js';
|
||||
import { CREATE as SQLITE_CREATE_PIC } from '../../providers/sqlite3/queries/pic.js';
|
||||
import processAvifImage from '../../util/processAvifImage.js';
|
||||
import newFullHash from '../../util/newFullHash.js';
|
||||
import request from '../request.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function addImage(pool) {
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
export default function addImage(provider) {
|
||||
const resolve = resolveArticleBySku(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
|
||||
const getSortSql = isSqlite ? SQLITE_GET_NEXT_SORT : MSSQL_GET_NEXT_SORT;
|
||||
const createPicSql = isSqlite ? SQLITE_CREATE_PIC : MSSQL_CREATE_PIC;
|
||||
const addArticlePicSql = isSqlite ? SQLITE_ADD_ARTICLEPIC : MSSQL_ADD_ARTICLEPIC;
|
||||
|
||||
/**
|
||||
* Adds an image to an article by its SKU, processing it into AVIF variants.
|
||||
@@ -14,50 +27,53 @@ export default function addImage(pool) {
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {string} sku - The article SKU.
|
||||
* @param {Buffer} image - The source image buffer.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{picId: number, sort: number, full: {w: number, h: number, size: number}, thumb: {w: number, h: number}, fullHash: string, thumbHash: string}>} The added image info.
|
||||
*
|
||||
* @example
|
||||
* const result = await store.article.addImage(42, 'SKU-1', imageBuffer);
|
||||
* // => { picId, sort, full: { w, h, size }, thumb: { w, h }, fullHash, thumbHash }
|
||||
*/
|
||||
return async (googleAuthId, sku, image) => {
|
||||
const { articleId } = await resolve(googleAuthId, sku);
|
||||
return async (googleAuthId, sku, image, tx) => {
|
||||
const runAdd = async (runner) => {
|
||||
const { articleId } = await resolve(googleAuthId, sku, runner);
|
||||
|
||||
const processed = await processAvifImage(image);
|
||||
const processed = await processAvifImage(image);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
const sortResult = await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.query(GET_NEXT_SORT);
|
||||
|
||||
const sort = sortResult.recordset[0].sort;
|
||||
const sortResult = await runner.query(getSortSql, { articleId });
|
||||
const sort = sortResult.rows[0].sort;
|
||||
const fullHash = newFullHash();
|
||||
|
||||
const picResult = await request(pool, tx)
|
||||
.input('w', processed.full.w)
|
||||
.input('h', processed.full.h)
|
||||
.input('size', processed.full.size)
|
||||
.input('hash', fullHash)
|
||||
.input('thumbw', processed.thumb.w)
|
||||
.input('thumbh', processed.thumb.h)
|
||||
.input('thumbhash', processed.thumb.hash)
|
||||
.input('source', '')
|
||||
.input('fullbinary', processed.full.buffer)
|
||||
.input('thumbbinary', processed.thumb.buffer)
|
||||
.query(CREATE_PIC);
|
||||
if (isSqlite) {
|
||||
if (runner.writePicFull) {
|
||||
await runner.writePicFull(fullHash, processed.full.buffer);
|
||||
await runner.writePicThumb(fullHash, processed.thumb.buffer);
|
||||
} else if (provider?.writePicFull) {
|
||||
await provider.writePicFull(fullHash, processed.full.buffer);
|
||||
await provider.writePicThumb(fullHash, processed.thumb.buffer);
|
||||
}
|
||||
}
|
||||
|
||||
const picId = picResult.recordset[0].id;
|
||||
const picResult = await runner.query(createPicSql, {
|
||||
w: processed.full.w,
|
||||
h: processed.full.h,
|
||||
size: processed.full.size,
|
||||
hash: fullHash,
|
||||
thumbw: processed.thumb.w,
|
||||
thumbh: processed.thumb.h,
|
||||
thumbhash: processed.thumb.hash,
|
||||
source: '',
|
||||
fullbinary: processed.full.buffer,
|
||||
thumbbinary: processed.thumb.buffer,
|
||||
});
|
||||
|
||||
await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.input('picId', picId)
|
||||
.input('sort', sort)
|
||||
.query(ADD_ARTICLEPIC);
|
||||
const picId = picResult.rows[0]?.id ?? picResult.insertId;
|
||||
|
||||
await tx.commit();
|
||||
await runner.query(addArticlePicSql, {
|
||||
articleId,
|
||||
picId,
|
||||
sort,
|
||||
});
|
||||
|
||||
return {
|
||||
picId,
|
||||
@@ -74,13 +90,15 @@ export default function addImage(pool) {
|
||||
fullHash,
|
||||
thumbHash: processed.thumb.hash,
|
||||
};
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
throw err;
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
return runAdd(makeRunner(provider, tx));
|
||||
}
|
||||
if (typeof provider?.transaction === 'function') {
|
||||
return provider.transaction(runAdd);
|
||||
}
|
||||
return runAdd(makeRunner(provider));
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user