From f9ea95155902641534b71bdd6000dddaeffbaf41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E8=BE=89?= Date: Wed, 25 Dec 2019 14:11:55 +0800 Subject: [PATCH 1/3] =?UTF-8?q?psd=E6=96=87=E4=BB=B6=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/pages.js | 80 ++++++++++++++++++++++++++++++++++++++++ app/router.js | 1 + app/service/pages.js | 6 +++ config/config.default.js | 1 + package.json | 4 +- 5 files changed, 91 insertions(+), 1 deletion(-) diff --git a/app/controller/pages.js b/app/controller/pages.js index 6de37f3..2e87e55 100644 --- a/app/controller/pages.js +++ b/app/controller/pages.js @@ -1,5 +1,10 @@ 'use strict' const co = require('co') +const PSD = require('psd') +const fs = require('fs') +const path = require('path') +const rimraf = require('rimraf') +const sendToWormhole = require('stream-wormhole') module.exports = app => { class PagesController extends app.Controller { @@ -448,6 +453,81 @@ module.exports = app => { ctx.validate(createRule) yield ctx.service.pages.updateFork(ctx.request.body) } + async psdToPage () { + const { ctx } = this; + let currentPathDir = `psd_image` + const SERVER_PATH = './' + fs.existsSync(path.join(SERVER_PATH, currentPathDir)) || fs.mkdirSync(path.join(SERVER_PATH, currentPathDir)) + let stream = await ctx.getFileStream() + let filename = stream.filename // stream对象也包含了文件名,大小等基本信息 + // 创建文件写入路径 + let target = path.join(SERVER_PATH, currentPathDir + '/'+ filename) + const result = await new Promise((resolve, reject) => { + // 创建文件写入流 + const remoteFileStrem = fs.createWriteStream(target) + // 以管道方式写入流 + stream.pipe(remoteFileStrem) + + let errFlag + // 监听error事件 + remoteFileStrem.on('error', err => { + errFlag = true + // 停止写入 + sendToWormhole(stream) + remoteFileStrem.destroy() + console.log(err) + reject(err) + }) + + // 监听写入完成事件 + remoteFileStrem.on('finish', () => { + if (errFlag) return + resolve({ filename, name: stream.fields.name }) + }) + }) + console.log(result) + if (!result.filename) return false + function binaryToBase(bitmap){ + return Buffer.from(bitmap, 'binary').toString('base64') + } + let psd = await PSD.open(path.join(SERVER_PATH, currentPathDir + '/'+ filename)) + let descendantsList = psd.tree().descendants() + descendantsList.reverse() + let psdSourceList = [] + for (var i = 0; i < descendantsList.length; i++) { + if (descendantsList[i].isGroup()) continue + if (!descendantsList[i].visible) continue + try { + await descendantsList[i].saveAsPng(path.join(SERVER_PATH, currentPathDir + `/${i}.png`)) + // var statInfo = fs.statSync(SERVER_PATH + `psd_image/${timeStr}/${i}.png`,) + // // console.log(i, statInfo); + let a = fs.readFileSync(SERVER_PATH + `psd_image/${i}.png`, 'binary') + rimraf(SERVER_PATH + `psd_image/${i}.png`, function (err) { // 删除当前目录下的 test.txt + console.log(err) + }); + // let image = descendantsList[i] + // console.log(i, image); + psdSourceList.push({ + ...descendantsList[i].export(), + type: 'picture', + imageSrc: SERVER_PATH + `psd_image/${i}.png`, + path: binaryToBase(a) + }) + } catch (e) { + // 转换不出来的图层先忽 + // console.log(e) + continue + } + } + rimraf(SERVER_PATH + `psd_image/`, function (err) { + // 删除当前目录 + // console.log(err) + }); + ctx.body = { + elements: psdSourceList, + document: psd.tree().export().document + } + } } return PagesController diff --git a/app/router.js b/app/router.js index 5afa1a8..64732bf 100755 --- a/app/router.js +++ b/app/router.js @@ -83,6 +83,7 @@ module.exports = app => { app.post(`/editor/pages/history-publish`, 'pages.historyPublish') app.post(`/editor/pages/history-to-draft`, 'pages.historyToDraft') app.post(`/editor/pages/update-fork`, 'pages.updateFork') + app.post(`/editor/pages/psd-to-page`, 'pages.psdToPage') // names app.post(`/editor/pages/getNameBykeys`, 'pages.getNameBykeys') diff --git a/app/service/pages.js b/app/service/pages.js index 305fb0c..a4953b7 100644 --- a/app/service/pages.js +++ b/app/service/pages.js @@ -126,6 +126,12 @@ module.exports = app => { list = list.map(item => { var it = item.dataValues || {} if (it.id) { + try { + let content = it.draft || it.content + it.psdList = JSON.parse(content).psdList || [] + } catch (error) { + it.psdList = [] + } it.isPublish = !it.draft delete it.content delete it.draft diff --git a/config/config.default.js b/config/config.default.js index 9c3b34c..ae7a42b 100755 --- a/config/config.default.js +++ b/config/config.default.js @@ -18,6 +18,7 @@ module.exports = appInfo => { fileExtensions: [ '.csv', '.txt', + '.psd' ] } // 自定义 middleware diff --git a/package.json b/package.json index ca52217..30006e0 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,9 @@ "nodemailer": "^4.0.1", "nunjucks": "^3.0.0", "opentsdb-node-client": "^1.0.1", - "stream-wormhole": "^1.1.0" + "stream-wormhole": "^1.1.0", + "psd": "^3.2.0", + "rimraf": "^3.0.0" }, "devDependencies": { "autod": "^2.8.0", From a8a5a7d426563f0ea3603f1de518ba42b1888963 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E8=BE=89?= Date: Mon, 30 Dec 2019 11:11:10 +0800 Subject: [PATCH 2/3] =?UTF-8?q?psd=E5=9B=BE=E5=B1=82=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E7=AB=AF=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/pages.js | 46 +++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/app/controller/pages.js b/app/controller/pages.js index 2e87e55..5729608 100644 --- a/app/controller/pages.js +++ b/app/controller/pages.js @@ -5,6 +5,7 @@ const fs = require('fs') const path = require('path') const rimraf = require('rimraf') const sendToWormhole = require('stream-wormhole') +const OSS = require('ali-oss') module.exports = app => { class PagesController extends app.Controller { @@ -453,6 +454,9 @@ module.exports = app => { ctx.validate(createRule) yield ctx.service.pages.updateFork(ctx.request.body) } + /** + * psd 文件解析 + */ async psdToPage () { const { ctx } = this; let currentPathDir = `psd_image` @@ -467,7 +471,6 @@ module.exports = app => { const remoteFileStrem = fs.createWriteStream(target) // 以管道方式写入流 stream.pipe(remoteFileStrem) - let errFlag // 监听error事件 remoteFileStrem.on('error', err => { @@ -478,7 +481,6 @@ module.exports = app => { console.log(err) reject(err) }) - // 监听写入完成事件 remoteFileStrem.on('finish', () => { if (errFlag) return @@ -487,9 +489,6 @@ module.exports = app => { }) console.log(result) if (!result.filename) return false - function binaryToBase(bitmap){ - return Buffer.from(bitmap, 'binary').toString('base64') - } let psd = await PSD.open(path.join(SERVER_PATH, currentPathDir + '/'+ filename)) let descendantsList = psd.tree().descendants() descendantsList.reverse() @@ -498,24 +497,22 @@ module.exports = app => { if (descendantsList[i].isGroup()) continue if (!descendantsList[i].visible) continue try { - await descendantsList[i].saveAsPng(path.join(SERVER_PATH, currentPathDir + `/${i}.png`)) - // var statInfo = fs.statSync(SERVER_PATH + `psd_image/${timeStr}/${i}.png`,) - // // console.log(i, statInfo); - let a = fs.readFileSync(SERVER_PATH + `psd_image/${i}.png`, 'binary') - rimraf(SERVER_PATH + `psd_image/${i}.png`, function (err) { // 删除当前目录下的 test.txt - console.log(err) - }); - // let image = descendantsList[i] - // console.log(i, image); + await descendantsList[i].saveAsPng(path.join(SERVER_PATH, currentPathDir + `/${i}.png`)) + let src = await this.upload(fs.createReadStream(SERVER_PATH + `psd_image/${i}.png`), [Date.now(), (Math.random() + 1) * 1000000000 | 0].map(v => v.toString(16)).join('') + '.png') + console.log('src', src) psdSourceList.push({ ...descendantsList[i].export(), type: 'picture', - imageSrc: SERVER_PATH + `psd_image/${i}.png`, - path: binaryToBase(a) + // imageSrc: SERVER_PATH + `psd_image/${i}.png`, + // path: binaryToBase(a) + src }) + rimraf(SERVER_PATH + `psd_image/${i}.png`, function (err) { // 删除当前目录下的 test.txt + console.log(err) + }) } catch (e) { // 转换不出来的图层先忽 - // console.log(e) + console.log(e) continue } } @@ -528,7 +525,20 @@ module.exports = app => { document: psd.tree().export().document } } + async upload (filedata, fileName) { + let ossClient = new OSS({ + region: app.config.appConfig.oss.region, + accessKeyId: app.config.appConfig.oss.accessKeyId, + accessKeySecret: app.config.appConfig.oss.accessKeySecret, + bucket: app.config.appConfig.oss.bucket, + }) + let result + await co(function * () { + result = yield ossClient.put(fileName, filedata) + }) + // console.log('result', result) + return (result && result.url.replace(/^http(?!s)/, 'https')) || '' + } } - return PagesController } From 0630e23397fe3d901ba12595b9d73be84d4f4418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E8=BE=89?= Date: Tue, 31 Dec 2019 11:02:00 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/pages.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/controller/pages.js b/app/controller/pages.js index 5729608..4bed395 100644 --- a/app/controller/pages.js +++ b/app/controller/pages.js @@ -497,8 +497,9 @@ module.exports = app => { if (descendantsList[i].isGroup()) continue if (!descendantsList[i].visible) continue try { - await descendantsList[i].saveAsPng(path.join(SERVER_PATH, currentPathDir + `/${i}.png`)) - let src = await this.upload(fs.createReadStream(SERVER_PATH + `psd_image/${i}.png`), [Date.now(), (Math.random() + 1) * 1000000000 | 0].map(v => v.toString(16)).join('') + '.png') + await descendantsList[i].saveAsPng(path.join(SERVER_PATH, currentPathDir + `/${i}.png`)) + const fName = `ml/psd-img/${[Date.now(), (Math.random() + 1) * 1000000000 | 0].map(v => v.toString(16)).join('')}.png` + let src = await this.upload(fs.createReadStream(SERVER_PATH + `psd_image/${i}.png`), fName) console.log('src', src) psdSourceList.push({ ...descendantsList[i].export(),