This commit is contained in:
seb
2026-08-01 01:36:49 +02:00
parent 243143e7eb
commit 671064bf3f
9 changed files with 181 additions and 5 deletions

View File

@@ -27,6 +27,8 @@ import googleAuthGetByEmail from './store/google_auth/getByEmail.js';
import googleAuthListAllowedEmails from './store/google_auth/listAllowedEmails.js';
import googleAuthRemoveAllowedEmail from './store/google_auth/removeAllowedEmail.js';
import googleAuthSetSelectedCompany from './store/google_auth/setSelectedCompany.js';
import picGetImageByHash from './store/pic/getImageByHash.js';
import picGetThumbByHash from './store/pic/getThumbByHash.js';
import storePurge from './store/purge.js';
import storeInitialize from './store/initialize.js';
@@ -84,6 +86,11 @@ export default class LibDashStore {
setSelectedCompany: googleAuthSetSelectedCompany(pool),
};
this.pic = {
getImageByHash: picGetImageByHash(pool),
getThumbByHash: picGetThumbByHash(pool),
};
this.purge = storePurge(pool);
this.initialize = storeInitialize(pool);

View File

@@ -91,7 +91,7 @@ BEGIN
thumbh INT NOT NULL,
thumbhash NVARCHAR(64) NOT NULL,
source NVARCHAR(MAX) NOT NULL,
[binary] VARBINARY(MAX) NOT NULL,
fullbinary VARBINARY(MAX) NOT NULL,
thumbbinary VARBINARY(MAX) NOT NULL
);
END

View File

@@ -1,9 +1,29 @@
export const CREATE = `
INSERT INTO pic (
w, h, size, hash, thumbw, thumbh, thumbhash, source, [binary], thumbbinary
w, h, size, hash, thumbw, thumbh, thumbhash, source, fullbinary, thumbbinary
)
OUTPUT INSERTED.id
VALUES (
@w, @h, @size, @hash, @thumbw, @thumbh, @thumbhash, @source, @binary, @thumbbinary
@w, @h, @size, @hash, @thumbw, @thumbh, @thumbhash, @source, @fullbinary, @thumbbinary
)
`;
export const GET_CATEGORY_ID_BY_HASH = `
SELECT TOP 1 ca.category_id AS categoryId
FROM pic p
INNER JOIN articlepic ap ON ap.pic_id = p.id
INNER JOIN categoryarticle ca ON ca.article_id = ap.article_id
WHERE p.hash = @hash
`;
export const GET_FULL_BY_HASH = `
SELECT w, h, size, fullbinary AS buffer
FROM pic
WHERE hash = @hash
`;
export const GET_THUMB_BY_HASH = `
SELECT thumbw AS w, thumbh AS h, thumbbinary AS buffer
FROM pic
WHERE hash = @hash
`;

View File

@@ -42,7 +42,7 @@ export default function addImage(pool) {
.input('thumbh', processed.thumb.h)
.input('thumbhash', processed.thumb.hash)
.input('source', '')
.input('binary', processed.full.buffer)
.input('fullbinary', processed.full.buffer)
.input('thumbbinary', processed.thumb.buffer)
.query(CREATE_PIC);

View File

@@ -0,0 +1,28 @@
import { GET_FULL_BY_HASH } from '../../sql/pic.js';
import resolvePicAccess from './resolvePicAccess.js';
export default function getImageByHash(pool) {
const authorize = resolvePicAccess(pool);
return async (googleAuthId, hash) => {
await authorize(googleAuthId, hash);
const result = await pool
.request()
.input('hash', hash)
.query(GET_FULL_BY_HASH);
const row = result.recordset[0];
if (!row?.buffer) {
throw new Error('Image not found');
}
return {
buffer: row.buffer,
w: row.w,
h: row.h,
size: row.size,
contentType: 'image/avif',
};
};
}

View File

@@ -0,0 +1,27 @@
import { GET_THUMB_BY_HASH } from '../../sql/pic.js';
import resolvePicAccess from './resolvePicAccess.js';
export default function getThumbByHash(pool) {
const authorize = resolvePicAccess(pool);
return async (googleAuthId, hash) => {
await authorize(googleAuthId, hash);
const result = await pool
.request()
.input('hash', hash)
.query(GET_THUMB_BY_HASH);
const row = result.recordset[0];
if (!row?.buffer) {
throw new Error('Image not found');
}
return {
buffer: row.buffer,
w: row.w,
h: row.h,
contentType: 'image/avif',
};
};
}

View File

@@ -0,0 +1,20 @@
import { GET_CATEGORY_ID_BY_HASH } from '../../sql/pic.js';
import resolveCategoryAccess from '../category/resolveCategoryAccess.js';
export default function resolvePicAccess(pool) {
const access = resolveCategoryAccess(pool);
return async (googleAuthId, hash) => {
const result = await pool
.request()
.input('hash', hash)
.query(GET_CATEGORY_ID_BY_HASH);
const categoryId = result.recordset[0]?.categoryId;
if (!categoryId) {
throw new Error('Image not found');
}
await access(googleAuthId, categoryId);
};
}