fix(group-album): validate image upload inputs
Reject unsupported media with a clear error before processing begins. Existing image upload behavior remains unchanged. Refs #310
This commit is contained in:
2 files changed
+38
-1
No files matched your search
@@ -221,8 +221,15 @@ export async function uploadImageToGroupAlbum(
|
|||||||
filePath: string,
|
filePath: string,
|
||||||
uin: string
|
uin: string
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const { loadBinarySource } = await import('../highway/utils');
|
const { detectImageFormat, loadBinarySource } = await import('../highway/utils');
|
||||||
const loaded = await loadBinarySource(filePath, 'album image');
|
const loaded = await loadBinarySource(filePath, 'album image');
|
||||||
|
const format = detectImageFormat(loaded.bytes);
|
||||||
|
const isJpeg = loaded.bytes.length >= 2
|
||||||
|
&& loaded.bytes[0] === 0xFF
|
||||||
|
&& loaded.bytes[1] === 0xD8;
|
||||||
|
if (format.format === 1000 && !isJpeg) {
|
||||||
|
throw new Error('群相册上传仅支持 JPEG、PNG、GIF、WebP 或 BMP 图片');
|
||||||
|
}
|
||||||
|
|
||||||
let tempFile: string | null = null;
|
let tempFile: string | null = null;
|
||||||
let actualPath = filePath;
|
let actualPath = filePath;
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||||
|
|
||||||
|
import { uploadImageToGroupAlbum } from '../../src/web/group-album';
|
||||||
|
import { RequestUtil } from '../../src/web/request-util';
|
||||||
|
|
||||||
|
describe('group album image upload', () => {
|
||||||
|
afterEach(() => {
|
||||||
|
vi.restoreAllMocks();
|
||||||
|
vi.unstubAllGlobals();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects an MP4 before starting a remote upload', async () => {
|
||||||
|
const request = vi.spyOn(RequestUtil, 'HttpGetJson')
|
||||||
|
.mockRejectedValue(new Error('unexpected remote upload'));
|
||||||
|
const fetch = vi.fn();
|
||||||
|
vi.stubGlobal('fetch', fetch);
|
||||||
|
|
||||||
|
await expect(uploadImageToGroupAlbum(
|
||||||
|
{ skey: 'SK', p_skey: 'PSK' },
|
||||||
|
'12345',
|
||||||
|
'album-id',
|
||||||
|
'album-name',
|
||||||
|
'base64://AAAAGGZ0eXBtcDQyAAAAAG1wNDJpc29t',
|
||||||
|
'10000',
|
||||||
|
)).rejects.toThrow('群相册上传仅支持 JPEG、PNG、GIF、WebP 或 BMP 图片');
|
||||||
|
|
||||||
|
expect(request).not.toHaveBeenCalled();
|
||||||
|
expect(fetch).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user