| 1 | import * as fs from 'fs'; |
| 2 | import sharp from 'sharp'; |
| 3 | import * as path from 'path'; |
| 4 | import requestNet from '../requestNet'; |
| 5 | |
| 6 | /** |
| 7 | * 获取文件内容 |
| 8 | * @param filePath 本地文件路径 |
| 9 | */ |
| 10 | export async function getFileContent(filePath: string): Promise<Buffer> { |
| 11 | try { |
| 12 | if (filePath.includes('https://') || filePath.includes('http://')) { |
| 13 | const res = await requestNet({ |
| 14 | url: filePath, |
| 15 | isReqFile: true, |
| 16 | }); |
| 17 | return res.data; |
| 18 | } else { |
| 19 | // 确保路径是绝对路径 |
| 20 | const absolutePath = path.resolve(filePath); |
| 21 | console.log('Reading file:', absolutePath); |
| 22 | |
| 23 | // 读取文件内容 |
| 24 | return await fs.promises.readFile(absolutePath); |
| 25 | } |
| 26 | } catch (error) { |
| 27 | console.error('Failed to read file:', error); |
| 28 | throw new Error(`读取文件失败: filePath`); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * 获取图片的基本信息 |
| 34 | * @param filePath |
| 35 | */ |
| 36 | export async function getImageBaseInfo(filePath: string) { |
| 37 | const buffer = await getFileContent(filePath); |
| 38 | const metadata = await sharp(buffer).metadata(); |
| 39 | |
| 40 | return { |
| 41 | width: metadata.width, |
| 42 | height: metadata.height, |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | export const CookieToString = (cookies: Electron.Cookie[]) => { |
| 47 | return cookies.map((cookie) => `${cookie.name}=${cookie.value}`).join('; '); |
| 48 | }; |
| 49 |