u
This commit is contained in:
@@ -1556,6 +1556,18 @@
|
||||
* type: string
|
||||
* sort:
|
||||
* type: integer
|
||||
* AttributeValuePic:
|
||||
* type: object
|
||||
* required: [fullHash, thumbW, thumbH, thumbHash]
|
||||
* properties:
|
||||
* fullHash:
|
||||
* type: string
|
||||
* thumbW:
|
||||
* type: integer
|
||||
* thumbH:
|
||||
* type: integer
|
||||
* thumbHash:
|
||||
* type: string
|
||||
* Attributename:
|
||||
* type: object
|
||||
* required: [id, name, sort, exclusive, values]
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import express from 'express';
|
||||
import * as db from '../../db/attributevalue.js';
|
||||
import { emit } from '../sse.js';
|
||||
|
||||
const AVIF_BODY_LIMIT = 10 * 1024 * 1024;
|
||||
|
||||
function requireSelectedCompany(req) {
|
||||
const companyId = req.user?.selectedCompanyId;
|
||||
if (companyId == null) {
|
||||
@@ -11,8 +14,10 @@ function requireSelectedCompany(req) {
|
||||
|
||||
function attributeValueErrorStatus(error) {
|
||||
if (error.message === 'No company selected') return 400;
|
||||
if (error.message === 'Unsupported image format' || error.message?.includes('unsupported image format')) return 400;
|
||||
if (error.message === 'Not authorized') return 403;
|
||||
if (error.message === 'Company not found' || error.message === 'Attribute name not found' || error.message === 'Attribute value not found') return 404;
|
||||
if (error.message === 'Image not found') return 404;
|
||||
if (error.message === 'Value required') return 400;
|
||||
if (error.message === 'Invalid attribute value order') return 400;
|
||||
if (error.message === 'Invalid id' || error.message === 'Invalid attributeId') return 400;
|
||||
@@ -301,4 +306,215 @@ export function register(app, basePath = '') {
|
||||
res.status(status).json({ error: message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /attributevalue/{id}/image:
|
||||
* post:
|
||||
* tags: [Article]
|
||||
* summary: Add or replace attribute value image
|
||||
* description: |
|
||||
* Uploads an image for the attribute value (AVIF, WebP, or octet-stream). Converted to AVIF full and thumbnail.
|
||||
* Replaces any existing image for this attribute value. Emits SSE signal `attributename`.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-signals: [attributename]
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* description: Attribute value id
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* image/avif:
|
||||
* schema:
|
||||
* type: string
|
||||
* format: binary
|
||||
* image/webp:
|
||||
* schema:
|
||||
* type: string
|
||||
* format: binary
|
||||
* application/octet-stream:
|
||||
* schema:
|
||||
* type: string
|
||||
* format: binary
|
||||
* responses:
|
||||
* 201:
|
||||
* description: Image stored and linked
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* attributevalue:
|
||||
* type: object
|
||||
* properties:
|
||||
* id:
|
||||
* type: integer
|
||||
* fullHash:
|
||||
* type: string
|
||||
* thumbW:
|
||||
* type: integer
|
||||
* thumbH:
|
||||
* type: integer
|
||||
* thumbHash:
|
||||
* type: string
|
||||
* pics:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/AttributeValuePic'
|
||||
* 400:
|
||||
* description: Missing body or invalid image
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 401:
|
||||
* description: Unauthorized
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 403:
|
||||
* description: Not authorized
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 404:
|
||||
* description: Attribute value not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* delete:
|
||||
* tags: [Article]
|
||||
* summary: Delete attribute value image
|
||||
* description: Removes the attribute value's image. Sets thumbW, thumbH, thumbHash back to null. Emits SSE signal `attributename`.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-signals: [attributename]
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* description: Attribute value id
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Image removed
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* attributevalue:
|
||||
* type: object
|
||||
* properties:
|
||||
* id:
|
||||
* type: integer
|
||||
* thumbW:
|
||||
* type: integer
|
||||
* nullable: true
|
||||
* thumbH:
|
||||
* type: integer
|
||||
* nullable: true
|
||||
* thumbHash:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* pics:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/AttributeValuePic'
|
||||
* 400:
|
||||
* description: No company selected or invalid id
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 401:
|
||||
* description: Unauthorized
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 403:
|
||||
* description: Not authorized
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 404:
|
||||
* description: Attribute value or image not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
|
||||
// #region POST /attributevalue/:id/image
|
||||
|
||||
app.post(
|
||||
`${basePath}/attributevalue/:id/image`,
|
||||
express.raw({ type: ['image/avif', 'image/webp', 'application/octet-stream'], limit: AVIF_BODY_LIMIT }),
|
||||
async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
const id = Number(req.params.id);
|
||||
if (!Number.isInteger(id) || id <= 0) {
|
||||
return res.status(400).json({ error: 'id is required' });
|
||||
}
|
||||
|
||||
const imageBuffer = Buffer.isBuffer(req.body) ? req.body : null;
|
||||
if (!imageBuffer?.length) {
|
||||
return res.status(400).json({ error: 'AVIF or WebP image body is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
const attributevalue = await db.addImage(req.user.id, id, imageBuffer);
|
||||
emit('attributename', { companyId: selected.companyId });
|
||||
res.status(201).json({ attributevalue });
|
||||
} catch (error) {
|
||||
const status = attributeValueErrorStatus(error);
|
||||
if (status === 500) console.error('Add attribute value image failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region DELETE /attributevalue/:id/image
|
||||
|
||||
app.delete(`${basePath}/attributevalue/:id/image`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
const id = Number(req.params.id);
|
||||
if (!Number.isInteger(id) || id <= 0) {
|
||||
return res.status(400).json({ error: 'id is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
const attributevalue = await db.deleteImage(req.user.id, id);
|
||||
emit('attributename', { companyId: selected.companyId });
|
||||
res.status(200).json({ attributevalue });
|
||||
} catch (error) {
|
||||
const status = attributeValueErrorStatus(error);
|
||||
if (status === 500) console.error('Delete attribute value image failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// #endregion
|
||||
}
|
||||
|
||||
|
||||
@@ -24,3 +24,14 @@ export async function remove(googleAuthId, id) {
|
||||
const store = await getStore();
|
||||
return store.attributevalue.delete(googleAuthId, id);
|
||||
}
|
||||
|
||||
export async function addImage(googleAuthId, attributeValueId, image) {
|
||||
const store = await getStore();
|
||||
return store.attributevalue.addImage(googleAuthId, attributeValueId, image);
|
||||
}
|
||||
|
||||
export async function deleteImage(googleAuthId, attributeValueId) {
|
||||
const store = await getStore();
|
||||
return store.attributevalue.deleteImage(googleAuthId, attributeValueId);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user