mirror of
https://framagit.org/les/gancio.git
synced 2025-02-01 09:02:01 +01:00
add image by url ...
dont know if this is a good idea (it opens a way to make http GET request by random users...)
This commit is contained in:
parent
41791346ee
commit
24bdff2ee7
3 changed files with 75 additions and 2 deletions
|
@ -96,7 +96,7 @@
|
|||
:on-change='uploadedFile'
|
||||
:multiple='false')
|
||||
i.el-icon-upload
|
||||
//- el-input(v-model='mediaUrl')
|
||||
el-input(v-model='image_url')
|
||||
el-button.mt-2.float-right(@click='done' :disabled='!couldProceed') {{edit?$t('common.edit'):$t('common.send')}}
|
||||
|
||||
</template>
|
||||
|
@ -171,7 +171,7 @@ export default {
|
|||
time: { start: '20:00', end: null },
|
||||
edit: false,
|
||||
loading: false,
|
||||
mediaUrl: '',
|
||||
image_url: '',
|
||||
queryTags: '',
|
||||
disableAddress: true
|
||||
}
|
||||
|
@ -350,6 +350,11 @@ export default {
|
|||
if (this.event.image) {
|
||||
formData.append('image', this.event.image.raw, this.event.image.name)
|
||||
}
|
||||
|
||||
if (this.image_url) {
|
||||
formData.append('image_url', this.image_url)
|
||||
}
|
||||
|
||||
formData.append('title', this.event.title)
|
||||
formData.append('place_name', this.event.place.name)
|
||||
formData.append('place_address', this.event.place.address)
|
||||
|
|
|
@ -5,6 +5,7 @@ const config = require('config')
|
|||
const fs = require('fs')
|
||||
const { Op } = require('sequelize')
|
||||
const _ = require('lodash')
|
||||
const helpers = require('../../helpers')
|
||||
const { event: Event, resource: Resource, tag: Tag, place: Place, notification: Notification } = require('../models')
|
||||
const Sequelize = require('sequelize')
|
||||
const exportController = require('./export')
|
||||
|
@ -222,6 +223,15 @@ const eventController = {
|
|||
eventDetails.image_path = req.file.filename
|
||||
}
|
||||
|
||||
// remote url
|
||||
if (body.image_url) {
|
||||
const img_path = await helpers.downloadImage(body.image_url)
|
||||
if (img_path) {
|
||||
debug('!!!! FINISH !!! ', img_path)
|
||||
eventDetails.image_path = img_path
|
||||
}
|
||||
}
|
||||
|
||||
const event = await Event.create(eventDetails)
|
||||
|
||||
// create place if needed
|
||||
|
|
|
@ -1,7 +1,13 @@
|
|||
const settingsController = require('./api/controller/settings')
|
||||
const acceptLanguage = require('accept-language')
|
||||
const moment = require('moment-timezone')
|
||||
const path = require('path')
|
||||
const sharp = require('sharp')
|
||||
const fs = require('fs')
|
||||
const axios = require('axios')
|
||||
const config = require('config')
|
||||
const crypto = require('crypto')
|
||||
const debug = require('debug')('helpers')
|
||||
const pkg = require('../package.json')
|
||||
|
||||
module.exports = {
|
||||
|
@ -26,6 +32,58 @@ module.exports = {
|
|||
moment.locale(req.settings.locale)
|
||||
moment.tz.setDefault(req.settings.instance_timezone)
|
||||
next()
|
||||
},
|
||||
|
||||
async downloadImage (url) {
|
||||
const response = await axios({
|
||||
url,
|
||||
method: 'GET',
|
||||
responseType: 'stream'
|
||||
})
|
||||
|
||||
const filename = crypto.randomBytes(16).toString('hex') + '.webp'
|
||||
const finalPath = path.resolve(config.upload_path, filename)
|
||||
const thumbPath = path.resolve(config.upload_path, 'thumb', filename)
|
||||
const outStream = fs.createWriteStream(finalPath)
|
||||
const thumbStream = fs.createWriteStream(thumbPath)
|
||||
|
||||
const resizer = sharp().resize(1200).webp({ quality: 95 })
|
||||
const thumbnailer = sharp().resize(400).webp({ quality: 90 })
|
||||
let onError = false
|
||||
const err = e => {
|
||||
if (onError) {
|
||||
return
|
||||
}
|
||||
onError = true
|
||||
debug(e)
|
||||
// cb(null)
|
||||
}
|
||||
|
||||
response.data
|
||||
.pipe(thumbnailer)
|
||||
.on('error', err)
|
||||
.pipe(thumbStream)
|
||||
.on('error', err)
|
||||
|
||||
response.data
|
||||
.pipe(resizer)
|
||||
.on('error', err)
|
||||
.pipe(outStream)
|
||||
.on('error', err)
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
outStream.on('error', reject)
|
||||
outStream.on('finish', function () {
|
||||
debug('outStream finish ', filename)
|
||||
resolve(filename)
|
||||
debug({
|
||||
destination: config.upload_path,
|
||||
filename,
|
||||
path: finalPath,
|
||||
size: outStream.bytesWritten
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue