mirror of
https://github.com/Kuingsmile/PicList.git
synced 2025-03-13 08:28:13 -04:00
🔨 Refactor(custom): refactored upload func
This commit is contained in:
parent
2079faa4c9
commit
fdae9bda2d
@ -1,9 +1,9 @@
|
|||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
import { BrowserWindow, clipboard, ipcMain, Notification, WebContents } from 'electron'
|
import { BrowserWindow, clipboard, ipcMain, Notification, WebContents } from 'electron'
|
||||||
import fs from 'fs-extra'
|
import fs from 'fs-extra'
|
||||||
import util from 'util'
|
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { IPicGo } from 'piclist'
|
import { IPicGo } from 'piclist'
|
||||||
|
import util from 'util'
|
||||||
import writeFile from 'write-file-atomic'
|
import writeFile from 'write-file-atomic'
|
||||||
|
|
||||||
import windowManager from 'apis/app/window/windowManager'
|
import windowManager from 'apis/app/window/windowManager'
|
||||||
@ -22,7 +22,6 @@ import { CLIPBOARD_IMAGE_FOLDER } from '#/utils/static'
|
|||||||
|
|
||||||
const waitForRename = (window: BrowserWindow, id: number): Promise<string | null> => {
|
const waitForRename = (window: BrowserWindow, id: number): Promise<string | null> => {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
const windowId = window.id
|
|
||||||
ipcMain.once(`${RENAME_FILE_NAME}${id}`, (_: Event, newName: string) => {
|
ipcMain.once(`${RENAME_FILE_NAME}${id}`, (_: Event, newName: string) => {
|
||||||
resolve(newName)
|
resolve(newName)
|
||||||
window.close()
|
window.close()
|
||||||
@ -30,20 +29,21 @@ const waitForRename = (window: BrowserWindow, id: number): Promise<string | null
|
|||||||
window.on('close', () => {
|
window.on('close', () => {
|
||||||
resolve(null)
|
resolve(null)
|
||||||
ipcMain.removeAllListeners(`${RENAME_FILE_NAME}${id}`)
|
ipcMain.removeAllListeners(`${RENAME_FILE_NAME}${id}`)
|
||||||
windowManager.deleteById(windowId)
|
windowManager.deleteById(window.id)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleTalkingData = (webContents: WebContents, options: IAnalyticsData) => {
|
const handleTalkingData = (webContents: WebContents, options: IAnalyticsData) => {
|
||||||
|
const { type, fromClipboard, count, duration } = options
|
||||||
const data: ITalkingDataOptions = {
|
const data: ITalkingDataOptions = {
|
||||||
EventId: 'upload',
|
EventId: 'upload',
|
||||||
Label: options.type,
|
Label: type,
|
||||||
MapKv: {
|
MapKv: {
|
||||||
by: options.fromClipboard ? 'clipboard' : 'files',
|
by: fromClipboard ? 'clipboard' : 'files',
|
||||||
count: options.count,
|
count,
|
||||||
duration: calcDurationRange(options.duration || 0),
|
duration: calcDurationRange(duration || 0),
|
||||||
type: options.type
|
type
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
webContents.send(TALKING_DATA_EVENT, data)
|
webContents.send(TALKING_DATA_EVENT, data)
|
||||||
@ -58,8 +58,7 @@ class Uploader {
|
|||||||
|
|
||||||
init() {
|
init() {
|
||||||
picgo.on(ICOREBuildInEvent.NOTIFICATION, (message: Electron.NotificationConstructorOptions | undefined) => {
|
picgo.on(ICOREBuildInEvent.NOTIFICATION, (message: Electron.NotificationConstructorOptions | undefined) => {
|
||||||
const notification = new Notification(message)
|
new Notification(message).show()
|
||||||
notification.show()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
picgo.on(ICOREBuildInEvent.UPLOAD_PROGRESS, (progress: any) => {
|
picgo.on(ICOREBuildInEvent.UPLOAD_PROGRESS, (progress: any) => {
|
||||||
@ -84,15 +83,11 @@ class Uploader {
|
|||||||
await Promise.all(
|
await Promise.all(
|
||||||
ctx.output.map(async (item, index) => {
|
ctx.output.map(async (item, index) => {
|
||||||
let name: undefined | string | null
|
let name: undefined | string | null
|
||||||
let fileName: string | undefined
|
const fileName = autoRename
|
||||||
if (autoRename) {
|
? `${dayjs().add(index, 'ms').format('YYYYMMDDHHmmSSS')}${item.extname}`
|
||||||
fileName = dayjs().add(index, 'ms').format('YYYYMMDDHHmmSSS') + item.extname
|
: item.fileName
|
||||||
} else {
|
|
||||||
fileName = item.fileName
|
|
||||||
}
|
|
||||||
if (rename) {
|
if (rename) {
|
||||||
const window = windowManager.create(IWindowList.RENAME_WINDOW)!
|
const window = windowManager.create(IWindowList.RENAME_WINDOW)!
|
||||||
logger.info('create rename window')
|
|
||||||
ipcMain.on(GET_RENAME_FILE_NAME, evt => {
|
ipcMain.on(GET_RENAME_FILE_NAME, evt => {
|
||||||
try {
|
try {
|
||||||
if (evt.sender.id === window.webContents.id) {
|
if (evt.sender.id === window.webContents.id) {
|
||||||
@ -118,61 +113,52 @@ class Uploader {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async getClipboardImagePath(): Promise<string | false> {
|
||||||
|
const imgPath = getClipboardFilePath()
|
||||||
|
if (imgPath) return imgPath
|
||||||
|
|
||||||
|
const nativeImage = clipboard.readImage()
|
||||||
|
if (nativeImage.isEmpty()) return false
|
||||||
|
|
||||||
|
const buffer = nativeImage.toPNG()
|
||||||
|
const baseDir = picgo.baseDir
|
||||||
|
const fileName = `${dayjs().format('YYYYMMDDHHmmSSS')}.png`
|
||||||
|
const filePath = path.join(baseDir, CLIPBOARD_IMAGE_FOLDER, fileName)
|
||||||
|
await writeFile(filePath, buffer)
|
||||||
|
return filePath
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* use electron's clipboard image to upload
|
* use electron's clipboard image to upload
|
||||||
*/
|
*/
|
||||||
async uploadWithBuildInClipboard(): Promise<ImgInfo[] | false> {
|
async uploadWithBuildInClipboard(): Promise<ImgInfo[] | false> {
|
||||||
let filePath = ''
|
let imgPath: string | false = false
|
||||||
try {
|
try {
|
||||||
const imgPath = getClipboardFilePath()
|
imgPath = await this.getClipboardImagePath()
|
||||||
if (!imgPath) {
|
if (!imgPath) return false
|
||||||
const nativeImage = clipboard.readImage()
|
return await this.upload([imgPath])
|
||||||
if (nativeImage.isEmpty()) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
const buffer = nativeImage.toPNG()
|
|
||||||
const baseDir = picgo.baseDir
|
|
||||||
const fileName = `${dayjs().format('YYYYMMDDHHmmSSS')}.png`
|
|
||||||
filePath = path.join(baseDir, CLIPBOARD_IMAGE_FOLDER, fileName)
|
|
||||||
await writeFile(filePath, buffer)
|
|
||||||
return await this.upload([filePath])
|
|
||||||
} else {
|
|
||||||
return await this.upload([imgPath])
|
|
||||||
}
|
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
return false
|
return false
|
||||||
} finally {
|
} finally {
|
||||||
if (filePath) {
|
if (imgPath) {
|
||||||
fs.remove(filePath)
|
fs.remove(imgPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async uploadWithBuildInClipboardReturnCtx(img?: IUploadOption, skipProcess = false): Promise<IPicGo | false> {
|
async uploadWithBuildInClipboardReturnCtx(img?: IUploadOption, skipProcess = false): Promise<IPicGo | false> {
|
||||||
let filePath = ''
|
let imgPath: string | false = false
|
||||||
try {
|
try {
|
||||||
const imgPath = getClipboardFilePath()
|
imgPath = await this.getClipboardImagePath()
|
||||||
if (!imgPath) {
|
if (!imgPath) return false
|
||||||
const nativeImage = clipboard.readImage()
|
return await this.uploadReturnCtx(img ?? [imgPath], skipProcess)
|
||||||
if (nativeImage.isEmpty()) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
const buffer = nativeImage.toPNG()
|
|
||||||
const baseDir = picgo.baseDir
|
|
||||||
const fileName = `${dayjs().format('YYYYMMDDHHmmSSS')}.png`
|
|
||||||
filePath = path.join(baseDir, CLIPBOARD_IMAGE_FOLDER, fileName)
|
|
||||||
await writeFile(filePath, buffer)
|
|
||||||
return await this.uploadReturnCtx(img ?? [filePath], skipProcess)
|
|
||||||
} else {
|
|
||||||
return await this.uploadReturnCtx(img ?? [imgPath], skipProcess)
|
|
||||||
}
|
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
return false
|
return false
|
||||||
} finally {
|
} finally {
|
||||||
if (filePath) {
|
if (imgPath) {
|
||||||
fs.remove(filePath)
|
fs.remove(imgPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -181,22 +167,22 @@ class Uploader {
|
|||||||
try {
|
try {
|
||||||
const startTime = Date.now()
|
const startTime = Date.now()
|
||||||
const ctx = await picgo.uploadReturnCtx(img, skipProcess)
|
const ctx = await picgo.uploadReturnCtx(img, skipProcess)
|
||||||
if (Array.isArray(ctx.output) && ctx.output.some((item: ImgInfo) => item.imgUrl)) {
|
if (!Array.isArray(ctx.output) || !ctx.output.some((item: ImgInfo) => item.imgUrl)) return false
|
||||||
if (this.webContents) {
|
|
||||||
handleTalkingData(this.webContents, {
|
if (this.webContents) {
|
||||||
fromClipboard: !img,
|
handleTalkingData(this.webContents, {
|
||||||
type: db.get(configPaths.picBed.uploader) || db.get(configPaths.picBed.current) || 'smms',
|
fromClipboard: !img,
|
||||||
count: img ? img.length : 1,
|
type: db.get(configPaths.picBed.uploader) || db.get(configPaths.picBed.current) || 'smms',
|
||||||
duration: Date.now() - startTime
|
count: img ? img.length : 1,
|
||||||
} as IAnalyticsData)
|
duration: Date.now() - startTime
|
||||||
}
|
} as IAnalyticsData)
|
||||||
ctx.output.forEach((item: ImgInfo) => {
|
|
||||||
item.config = JSON.parse(JSON.stringify(db.get(`picBed.${item.type}`)))
|
|
||||||
})
|
|
||||||
return ctx
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx.output.forEach((item: ImgInfo) => {
|
||||||
|
item.config = JSON.parse(JSON.stringify(db.get(`picBed.${item.type}`)))
|
||||||
|
})
|
||||||
|
|
||||||
|
return ctx
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@ -216,22 +202,20 @@ class Uploader {
|
|||||||
try {
|
try {
|
||||||
const startTime = Date.now()
|
const startTime = Date.now()
|
||||||
const output = await picgo.upload(img)
|
const output = await picgo.upload(img)
|
||||||
if (Array.isArray(output) && output.some((item: ImgInfo) => item.imgUrl)) {
|
if (!Array.isArray(output) || !output.some((item: ImgInfo) => item.imgUrl)) return false
|
||||||
if (this.webContents) {
|
|
||||||
handleTalkingData(this.webContents, {
|
if (this.webContents) {
|
||||||
fromClipboard: !img,
|
handleTalkingData(this.webContents, {
|
||||||
type: db.get(configPaths.picBed.uploader) || db.get(configPaths.picBed.current) || 'smms',
|
fromClipboard: !img,
|
||||||
count: img ? img.length : 1,
|
type: db.get(configPaths.picBed.uploader) || db.get(configPaths.picBed.current) || 'smms',
|
||||||
duration: Date.now() - startTime
|
count: img ? img.length : 1,
|
||||||
} as IAnalyticsData)
|
duration: Date.now() - startTime
|
||||||
}
|
} as IAnalyticsData)
|
||||||
output.forEach((item: ImgInfo) => {
|
|
||||||
item.config = JSON.parse(JSON.stringify(db.get(`picBed.${item.type}`)))
|
|
||||||
})
|
|
||||||
return output.filter(item => item.imgUrl)
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
output.forEach((item: ImgInfo) => {
|
||||||
|
item.config = JSON.parse(JSON.stringify(db.get(`picBed.${item.type}`)))
|
||||||
|
})
|
||||||
|
return output.filter(item => item.imgUrl)
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
@ -21,14 +21,10 @@ const checkLogFileIsLarge = (logPath: string): CheckLogFileResult => {
|
|||||||
logFileSizeLimit: DEFAULT_LOG_FILE_SIZE_LIMIT
|
logFileSizeLimit: DEFAULT_LOG_FILE_SIZE_LIMIT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return {
|
return { isLarge: false }
|
||||||
isLarge: false
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e)
|
console.log(e)
|
||||||
return {
|
return { isLarge: true }
|
||||||
isLarge: true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,9 +45,7 @@ const recreateLogFile = (logPath: string): void => {
|
|||||||
const getLogger = (logPath: string, logType: string) => {
|
const getLogger = (logPath: string, logType: string) => {
|
||||||
let hasUncathcedError = false
|
let hasUncathcedError = false
|
||||||
try {
|
try {
|
||||||
if (!fs.existsSync(logPath)) {
|
fs.ensureFileSync(logPath)
|
||||||
fs.ensureFileSync(logPath)
|
|
||||||
}
|
|
||||||
if (checkLogFileIsLarge(logPath).isLarge) {
|
if (checkLogFileIsLarge(logPath).isLarge) {
|
||||||
recreateLogFile(logPath)
|
recreateLogFile(logPath)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user