sqlite
This commit is contained in:
@@ -1,12 +1,25 @@
|
||||
import { ADD as ADD_CATEGORYPIC, GET_NEXT_SORT } from '../../providers/mssql/queries/categorypic.js';
|
||||
import { CREATE as CREATE_PIC } from '../../providers/mssql/queries/pic.js';
|
||||
import {
|
||||
ADD as MSSQL_ADD_CATEGORYPIC,
|
||||
GET_NEXT_SORT as MSSQL_GET_NEXT_SORT,
|
||||
} from '../../providers/mssql/queries/categorypic.js';
|
||||
import {
|
||||
ADD as SQLITE_ADD_CATEGORYPIC,
|
||||
GET_NEXT_SORT as SQLITE_GET_NEXT_SORT,
|
||||
} from '../../providers/sqlite3/queries/categorypic.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 resolveCategoryAccess from './resolveCategoryAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function addImage(pool) {
|
||||
const access = resolveCategoryAccess(pool);
|
||||
export default function addImage(provider) {
|
||||
const access = resolveCategoryAccess(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 addCategoryPicSql = isSqlite ? SQLITE_ADD_CATEGORYPIC : MSSQL_ADD_CATEGORYPIC;
|
||||
|
||||
/**
|
||||
* Adds an image to a category, processing it into AVIF full and thumb variants.
|
||||
@@ -14,50 +27,53 @@ export default function addImage(pool) {
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} categoryId - The id of the category.
|
||||
* @param {Buffer} image - The source image buffer.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{id: number, fullHash: string, thumbW: number, thumbH: number, thumbHash: string}>} The added image info.
|
||||
*
|
||||
* @example
|
||||
* const result = await store.category.addImage(42, 5, imageBuffer);
|
||||
* // => { id, fullHash, thumbW, thumbH, thumbHash }
|
||||
*/
|
||||
return async (googleAuthId, categoryId, image) => {
|
||||
await access(googleAuthId, categoryId);
|
||||
return async (googleAuthId, categoryId, image, tx) => {
|
||||
const runAdd = async (runner) => {
|
||||
await access(googleAuthId, categoryId, 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('categoryId', categoryId)
|
||||
.query(GET_NEXT_SORT);
|
||||
|
||||
const sort = sortResult.recordset[0].sort;
|
||||
const sortResult = await runner.query(getSortSql, { categoryId });
|
||||
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('categoryId', categoryId)
|
||||
.input('picId', picId)
|
||||
.input('sort', sort)
|
||||
.query(ADD_CATEGORYPIC);
|
||||
const picId = picResult.rows[0]?.id ?? picResult.insertId;
|
||||
|
||||
await tx.commit();
|
||||
await runner.query(addCategoryPicSql, {
|
||||
categoryId,
|
||||
picId,
|
||||
sort,
|
||||
});
|
||||
|
||||
return {
|
||||
id: categoryId,
|
||||
@@ -66,13 +82,14 @@ export default function addImage(pool) {
|
||||
thumbH: processed.thumb.h,
|
||||
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