|
@@ -6,12 +6,14 @@ const config = require('./config.js')
|
|
|
const logger = require('./logger.js')('image.js')
|
|
|
const downloadLogger = require('./logger.js')('download')
|
|
|
const upyun = require('./upyun.js')
|
|
|
+const PromisePool = require('./promise-pool.js')
|
|
|
const fs = require('fs')
|
|
|
const path = require('path')
|
|
|
const readline = require('readline')
|
|
|
const sizeOf = require('image-size')
|
|
|
const mustache = require('mustache')
|
|
|
const mkdirp = require('mkdirp')
|
|
|
+const moment = require('moment')
|
|
|
const gm = config.imagemagick != undefined ? require('gm').subClass({
|
|
|
imageMagick: true,
|
|
|
appPath: config.imagemagick
|
|
@@ -38,6 +40,13 @@ class executor extends EventEmitter {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+ async checkFile(url, client) {
|
|
|
+ let size = sizeOf(await client.download(url))
|
|
|
+ if (size.width == 0 || size.height == 0) {
|
|
|
+ throw 'invalid image data:' + url
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
async addWatermark(image, file, student, index, showMarker) {
|
|
|
let fontFile = config.watermark.fontFile
|
|
|
let color = config.watermark.color
|
|
@@ -282,6 +291,122 @@ class executor extends EventEmitter {
|
|
|
this.emit('error', error)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ async checkSlice(dir, concurrent) {
|
|
|
+ let bucket = env.server.bucketPrefix + '-slice'
|
|
|
+ let client = upyun(bucket, config.upyun.operator, config.upyun.password)
|
|
|
+ if (env.server.upyunDomain && env.server.upyunDomain != '') {
|
|
|
+ //局域网模式修改图片服务器地址
|
|
|
+ client.setDomain(env.server.upyunDomain)
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ let logFile = path.join(dir, 'result.txt')
|
|
|
+ fs.writeFileSync(logFile, moment().format('YYYY-MM-DD HH:mm:ss') + ', examId=' + env.examId + '\r\n')
|
|
|
+
|
|
|
+ let totalCount = await api.countStudents(env.examId, true, false)
|
|
|
+ this.emit('total', totalCount)
|
|
|
+
|
|
|
+ let self = this
|
|
|
+ let count = 0
|
|
|
+ let pageNumber = 0
|
|
|
+ let pool = PromisePool.create(concurrent, function(student) {
|
|
|
+ student.examId = env.examId
|
|
|
+ student.promises = []
|
|
|
+ for (let i = 1; i <= student.sliceCount; i++) {
|
|
|
+ student.index = i
|
|
|
+ let url = mustache.render(config.imageUrl.slice, student)
|
|
|
+ student.promises.push(new Promise(resolved => {
|
|
|
+ self.checkFile(url, client).then(() => {
|
|
|
+ //fs.appendFileSync(logFile, url + ': success\r\n')
|
|
|
+ }).catch(() => {
|
|
|
+ fs.appendFileSync(logFile, url + ': error\r\n')
|
|
|
+ }).finally(() => {
|
|
|
+ resolved()
|
|
|
+ })
|
|
|
+ }))
|
|
|
+ }
|
|
|
+ return Promise.all(student.promises)
|
|
|
+ })
|
|
|
+ pool.on('count', function(offset) {
|
|
|
+ self.emit('count', count + offset)
|
|
|
+ })
|
|
|
+
|
|
|
+ this.emit('count', 0)
|
|
|
+ for (;;) {
|
|
|
+ pageNumber++
|
|
|
+ let array = await api.getStudents(env.examId, pageNumber, 200, true, false, false, false)
|
|
|
+ if (array == undefined || array.length == 0) {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ await pool.start(array)
|
|
|
+ count += array.length
|
|
|
+ }
|
|
|
+ this.emit('finish')
|
|
|
+ fs.appendFileSync(logFile, moment().format('YYYY-MM-DD HH:mm:ss') + ', examId=' + env.examId)
|
|
|
+ } catch (error) {
|
|
|
+ logger.error('check slice error:' + error)
|
|
|
+ logger.error(error)
|
|
|
+ this.emit('error', error)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async checkSliceSerial(dir) {
|
|
|
+ let bucket = env.server.bucketPrefix + '-slice'
|
|
|
+ let client = upyun(bucket, config.upyun.operator, config.upyun.password)
|
|
|
+ if (env.server.upyunDomain && env.server.upyunDomain != '') {
|
|
|
+ //局域网模式修改图片服务器地址
|
|
|
+ client.setDomain(env.server.upyunDomain)
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ let logFile = path.join(dir, 'result.txt')
|
|
|
+ fs.writeFileSync(logFile, moment().format('YYYY-MM-DD HH:mm:ss') + ', examId=' + env.examId + '\r\n')
|
|
|
+
|
|
|
+ let totalCount = await api.countStudents(env.examId, true, false)
|
|
|
+ this.emit('total', totalCount)
|
|
|
+
|
|
|
+ let self = this
|
|
|
+ let count = 0
|
|
|
+ let pageNumber = 0
|
|
|
+ this.emit('count', 0)
|
|
|
+ for (;;) {
|
|
|
+ pageNumber++
|
|
|
+
|
|
|
+ let array = await api.getStudents(env.examId, pageNumber, 100, true, false, false, false)
|
|
|
+ if (array == undefined || array.length == 0) {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ for (let i = 0; i < array.length; i++) {
|
|
|
+ let student = array[i]
|
|
|
+ student.examId = env.examId
|
|
|
+ student.promises = []
|
|
|
+ for (let i = 1; i <= student.sliceCount; i++) {
|
|
|
+ student.index = i
|
|
|
+ let url = mustache.render(config.imageUrl.slice, student)
|
|
|
+ student.promises.push(new Promise(resolved => {
|
|
|
+ self.checkFile(url, client).then(() => {
|
|
|
+ //fs.appendFileSync(logFile, url + ': success\r\n')
|
|
|
+ }).catch(() => {
|
|
|
+ fs.appendFileSync(logFile, url + ': error\r\n')
|
|
|
+ }).finally(() => {
|
|
|
+ resolved()
|
|
|
+ })
|
|
|
+ }))
|
|
|
+ }
|
|
|
+ await Promise.all(student.promises)
|
|
|
+ count++
|
|
|
+ this.emit('count', count)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ this.emit('finish')
|
|
|
+ fs.appendFileSync(logFile, moment().format('YYYY-MM-DD HH:mm:ss') + ', examId=' + env.examId)
|
|
|
+ } catch (error) {
|
|
|
+ logger.error('check slice error:' + error)
|
|
|
+ logger.error(error)
|
|
|
+ this.emit('error', error)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
module.exports = function() {
|