Finished: rename file before upload

This commit is contained in:
Molunerfinn
2018-05-08 14:04:43 +08:00
parent 757a7d1260
commit 74b94feb74
6 changed files with 146 additions and 10 deletions

View File

@@ -2,17 +2,51 @@ import fs from 'fs-extra'
import path from 'path'
import sizeOf from 'image-size'
import fecha from 'fecha'
import { BrowserWindow, ipcMain } from 'electron'
import db from '../../datastore/index.js'
const renameURL = process.env.NODE_ENV === 'development' ? `http://localhost:9080/#rename-page` : `file://${__dirname}/index.html#rename-page`
const createRenameWindow = () => {
let options = {
height: 175,
width: 300,
show: true,
fullscreenable: false,
resizable: false,
vibrancy: 'ultra-dark',
webPreferences: {
backgroundThrottling: false
}
}
if (process.platform === 'win32') {
options.show = true
options.backgroundColor = '#3f3c37'
}
const window = new BrowserWindow(options)
window.loadURL(renameURL)
return window
}
const imgFromPath = async (imgPath) => {
let results = []
let rename = db.read().get('picBed.rename').value()
await Promise.all(imgPath.map(async item => {
let name
let fileName = path.basename(item)
if (rename) {
const window = createRenameWindow()
await waitForShow(window.webContents)
window.webContents.send('rename', fileName, window.webContents.id)
name = await waitForRename(window, window.webContents.id)
}
let buffer = await fs.readFile(item)
let base64Image = Buffer.from(buffer, 'binary').toString('base64')
let fileName = path.basename(item)
let imgSize = sizeOf(item)
results.push({
base64Image,
fileName,
fileName: name || fileName,
width: imgSize.width,
height: imgSize.height,
extname: path.extname(item)
@@ -21,13 +55,21 @@ const imgFromPath = async (imgPath) => {
return results
}
const imgFromClipboard = (file) => {
const imgFromClipboard = async (file) => {
let result = []
let rename = db.read().get('picBed.rename').value()
if (file !== null) {
const today = fecha.format(new Date(), 'YYYYMMDDHHmmss')
let name
const today = fecha.format(new Date(), 'YYYYMMDDHHmmss') + '.png'
if (rename) {
const window = createRenameWindow()
await waitForShow(window.webContents)
window.webContents.send('rename', today, window.webContents.id)
name = await waitForRename(window, window.webContents.id)
}
result.push({
base64Image: file.imgUrl.replace(/^data\S+,/, ''),
fileName: `${today}.png`,
fileName: name || today,
width: file.width,
height: file.height,
extname: '.png'
@@ -38,10 +80,18 @@ const imgFromClipboard = (file) => {
const imgFromUploader = async (files) => {
let results = []
let rename = db.read().get('picBed.rename').value()
await Promise.all(files.map(async item => {
let name
if (rename) {
const window = createRenameWindow()
await waitForShow(window.webContents)
window.webContents.send('rename', item.name, window.webContents.id)
name = await waitForRename(window, window.webContents.id)
}
let buffer = await fs.readFile(item.path)
let base64Image = Buffer.from(buffer, 'binary').toString('base64')
let fileName = item.name
let fileName = name || item.name
let imgSize = sizeOf(item.path)
results.push({
base64Image,
@@ -54,6 +104,27 @@ const imgFromUploader = async (files) => {
return results
}
const waitForShow = (webcontent) => {
return new Promise((resolve, reject) => {
webcontent.on('dom-ready', () => {
resolve()
})
})
}
const waitForRename = (window, id) => {
return new Promise((resolve, reject) => {
ipcMain.once(`rename${id}`, (evt, newName) => {
resolve(newName)
window.hide()
})
window.on('close', () => {
resolve(null)
ipcMain.removeAllListeners(`rename${id}`)
})
})
}
export {
imgFromPath,
imgFromClipboard,

View File

@@ -63,7 +63,7 @@ const postOptions = (fileName, signature, imgBase64) => {
} else {
return {
method: 'PUT',
url: `http://${options.bucket}.cos.${options.area}.myqcloud.com/${path}${fileName}`,
url: `http://${options.bucket}.cos.${options.area}.myqcloud.com/${path}${encodeURI(fileName)}`,
headers: {
Host: `${options.bucket}.cos.${options.area}.myqcloud.com`,
Authorization: `q-sign-algorithm=sha1&q-ak=${options.secretId}&q-sign-time=${signature.signTime}&q-key-time=${signature.signTime}&q-header-list=host&q-url-param-list=&q-signature=${signature.signature}`,