Skip to content

Commit

Permalink
Resize an uploaded image
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtuGit committed Mar 12, 2021
1 parent 25a6e06 commit 008968a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
38 changes: 38 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"core-js": "^3.8.3",
"express": "^4.17.1",
"firebase": "^8.2.7",
"image-blob-reduce": "^2.2.2",
"js-cookie": "^2.2.1",
"lodash.orderby": "^4.6.0",
"nuxt": "^2.14.12",
Expand Down
26 changes: 18 additions & 8 deletions plugins/api-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
*/

import { authenticate } from 'pixabay-api'

import imageBlobReduce from 'image-blob-reduce'
const { searchImages } = authenticate(process.env.pixabayKey)
const reduce = imageBlobReduce()

const fakeRequestPromise = (delay = 1000) => {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -119,22 +122,29 @@ function getStorageDirName() {
else return 'demo'
}

// ToDo: Resize an uploaded image
// ToDo: Improve readability and error handling
function uploadURLToStorage(url) {
const that = this
const dirName = getStorageDirName.call(this)
const fileName = url.split('/').slice(-1)[0]
return new Promise(function (resolve, reject) {
getFileBlob(url, (blob) => {
try {
const path = `${dirName}/${fileName}`
const storageRef = that.$fire.storage.ref().child(path)
storageRef.put(blob).then(function (snapshot) {
storageRef.getDownloadURL().then(function (url) {
const res = { imagePath: path, url }
resolve(res)
reduce
.toBlob(blob, { max: 400 })
.then((blob) => {
return blob
})
.then((blobResized) => {
const path = `${dirName}/${fileName}`
const storageRef = that.$fire.storage.ref().child(path)
storageRef.put(blobResized).then(function (snapshot) {
storageRef.getDownloadURL().then(function (url) {
const res = { imagePath: path, url }
resolve(res)
})
})
})
})
} catch (e) {
alert(e.message)
}
Expand Down

0 comments on commit 008968a

Please sign in to comment.