sqlite
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
import { GET_FULL_BY_HASH } from '../../providers/mssql/queries/pic.js';
|
||||
import { GET_FULL_BY_HASH as MSSQL_GET } from '../../providers/mssql/queries/pic.js';
|
||||
import { GET_FULL_BY_HASH as SQLITE_GET } from '../../providers/sqlite3/queries/pic.js';
|
||||
import resolvePicAccess from './resolvePicAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function getImageByHash(pool) {
|
||||
const authorize = resolvePicAccess(pool);
|
||||
export default function getImageByHash(provider) {
|
||||
const authorize = resolvePicAccess(provider);
|
||||
const sql = provider?.dialect === 'sqlite3' ? SQLITE_GET : MSSQL_GET;
|
||||
|
||||
/**
|
||||
* Returns the full-size image (AVIF) for a given hash.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {string} fullHash - The full image hash.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{buffer: Buffer, w: number, h: number, size: number, contentType: string}>} The full image.
|
||||
* @throws {Error} If the image is not found.
|
||||
*
|
||||
@@ -16,21 +20,30 @@ export default function getImageByHash(pool) {
|
||||
* const image = await store.pic.getImageByHash(42, 'abc123');
|
||||
* // => { buffer, w, h, size, contentType: 'image/avif' }
|
||||
*/
|
||||
return async (googleAuthId, fullHash) => {
|
||||
await authorize(googleAuthId, fullHash);
|
||||
return async (googleAuthId, fullHash, tx) => {
|
||||
await authorize(googleAuthId, fullHash, tx);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('hash', fullHash)
|
||||
.query(GET_FULL_BY_HASH);
|
||||
const runner = makeRunner(provider, tx);
|
||||
const result = await runner.query(sql, { hash: fullHash });
|
||||
|
||||
const row = result.recordset[0];
|
||||
if (!row?.buffer) {
|
||||
const row = result.rows[0];
|
||||
if (!row) {
|
||||
throw new Error('Image not found');
|
||||
}
|
||||
|
||||
let buffer;
|
||||
if (provider?.dialect === 'sqlite3') {
|
||||
buffer = await (runner.readPicFull ? runner.readPicFull(fullHash) : provider.readPicFull?.(fullHash));
|
||||
} else {
|
||||
buffer = row.buffer ?? row.fullbinary;
|
||||
}
|
||||
|
||||
if (!buffer) {
|
||||
throw new Error('Image not found');
|
||||
}
|
||||
|
||||
return {
|
||||
buffer: row.buffer,
|
||||
buffer,
|
||||
w: row.w,
|
||||
h: row.h,
|
||||
size: row.size,
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
import { GET_THUMB_BY_HASH } from '../../providers/mssql/queries/pic.js';
|
||||
import { GET_THUMB_BY_HASH as MSSQL_GET } from '../../providers/mssql/queries/pic.js';
|
||||
import { GET_THUMB_BY_HASH as SQLITE_GET } from '../../providers/sqlite3/queries/pic.js';
|
||||
import resolvePicAccess from './resolvePicAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function getThumbByHash(pool) {
|
||||
const authorize = resolvePicAccess(pool);
|
||||
export default function getThumbByHash(provider) {
|
||||
const authorize = resolvePicAccess(provider);
|
||||
const sql = provider?.dialect === 'sqlite3' ? SQLITE_GET : MSSQL_GET;
|
||||
|
||||
/**
|
||||
* Returns the thumbnail image (AVIF) for a given hash.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {string} fullHash - The full image hash.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{buffer: Buffer, w: number, h: number, contentType: string}>} The thumbnail image.
|
||||
* @throws {Error} If the image is not found.
|
||||
*
|
||||
@@ -16,21 +20,30 @@ export default function getThumbByHash(pool) {
|
||||
* const thumb = await store.pic.getThumbByHash(42, 'abc123');
|
||||
* // => { buffer, w, h, contentType: 'image/avif' }
|
||||
*/
|
||||
return async (googleAuthId, fullHash) => {
|
||||
await authorize(googleAuthId, fullHash);
|
||||
return async (googleAuthId, fullHash, tx) => {
|
||||
await authorize(googleAuthId, fullHash, tx);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('hash', fullHash)
|
||||
.query(GET_THUMB_BY_HASH);
|
||||
const runner = makeRunner(provider, tx);
|
||||
const result = await runner.query(sql, { hash: fullHash });
|
||||
|
||||
const row = result.recordset[0];
|
||||
if (!row?.buffer) {
|
||||
const row = result.rows[0];
|
||||
if (!row) {
|
||||
throw new Error('Image not found');
|
||||
}
|
||||
|
||||
let buffer;
|
||||
if (provider?.dialect === 'sqlite3') {
|
||||
buffer = await (runner.readPicThumb ? runner.readPicThumb(fullHash) : provider.readPicThumb?.(fullHash));
|
||||
} else {
|
||||
buffer = row.buffer ?? row.thumbbinary;
|
||||
}
|
||||
|
||||
if (!buffer) {
|
||||
throw new Error('Image not found');
|
||||
}
|
||||
|
||||
return {
|
||||
buffer: row.buffer,
|
||||
buffer,
|
||||
w: row.w,
|
||||
h: row.h,
|
||||
contentType: 'image/avif',
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import { GET_CATEGORY_ID_BY_HASH } from '../../providers/mssql/queries/pic.js';
|
||||
import { GET_CATEGORY_ID_BY_HASH as MSSQL_GET } from '../../providers/mssql/queries/pic.js';
|
||||
import { GET_CATEGORY_ID_BY_HASH as SQLITE_GET } from '../../providers/sqlite3/queries/pic.js';
|
||||
import resolveCategoryAccess from '../category/resolveCategoryAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function resolvePicAccess(pool) {
|
||||
const access = resolveCategoryAccess(pool);
|
||||
export default function resolvePicAccess(provider) {
|
||||
const access = resolveCategoryAccess(provider);
|
||||
const sql = provider?.dialect === 'sqlite3' ? SQLITE_GET : MSSQL_GET;
|
||||
|
||||
return async (googleAuthId, fullHash) => {
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('hash', fullHash)
|
||||
.query(GET_CATEGORY_ID_BY_HASH);
|
||||
return async (googleAuthId, fullHash, tx) => {
|
||||
const result = await makeRunner(provider, tx).query(sql, { hash: fullHash });
|
||||
|
||||
const categoryId = result.recordset[0]?.categoryId;
|
||||
const categoryId = result.rows[0]?.categoryId;
|
||||
if (!categoryId) {
|
||||
throw new Error('Image not found');
|
||||
}
|
||||
|
||||
await access(googleAuthId, categoryId);
|
||||
await access(googleAuthId, categoryId, tx);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user