Skip to content

Commit

Permalink
Merge pull request #69 from ArtuGit/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ArtuGit authored Mar 16, 2021
2 parents ad6c1c1 + 6208b97 commit a87ae26
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 79 deletions.
2 changes: 1 addition & 1 deletion components/collections/Collection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export default {
break
case 'test':
this.toggleLoading()
this.$store.dispatch('collections/test').then(() => {
this.$store.dispatch('collections/test', this.collection).then(() => {
this.toggleLoading()
})
break
Expand Down
88 changes: 55 additions & 33 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 @@ -25,6 +25,7 @@
"@nuxtjs/axios": "^5.13.1",
"@nuxtjs/dotenv": "^1.4.1",
"@nuxtjs/firebase": "^7.5.0",
"browser-image-compression": "^1.0.14",
"core-js": "^3.9.1",
"firebase": "^8.3.0",
"image-blob-reduce": "^2.2.2",
Expand Down
64 changes: 23 additions & 41 deletions plugins/api-helpers.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/*
* Developed by Artu, https://github.com/ArtuGit
* Copyleft 2020-2021.
* Copyleft 2020-2021.
*/

import { authenticate } from 'pixabay-api'

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

const fakeRequestPromise = (delay = 1000) => {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -99,16 +97,6 @@ function makeFBQuery(context, pStr) {
return rStr
}

function getFileBlob(url, cb) {
const xhr = new XMLHttpRequest()
xhr.open('GET', url)
xhr.responseType = 'blob'
xhr.addEventListener('load', function () {
cb(xhr.response)
})
xhr.send()
}

function getStorageDirName() {
let isAdmin, uuid
if (this.getters) {
Expand All @@ -122,34 +110,28 @@ function getStorageDirName() {
else return 'demo'
}

// 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 {
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)
}
async function uploadURLToStorage(url) {
try {
const dirName = getStorageDirName.call(this)
const fileName = url.split('/').slice(-1)[0]
const path = `${dirName}/${fileName}`
let blob = await this.$axios.$get(url, {
baseURL: null,
responseType: 'blob',
})
})
blob = await imageCompression(blob, {
maxSizeMB: 0.33,
maxWidthOrHeight: 400,
useWebWorker: true,
})
const storageRef = await this.$fire.storage.ref().child(path)
await storageRef.put(blob)
const urlRet = await storageRef.getDownloadURL()
return { imagePath: path, url: urlRet }
} catch (error) {
// eslint-disable-next-line
console.error('Error in the image handling ', error)
}
}

function deleteFileOnStorage(path) {
Expand Down
9 changes: 5 additions & 4 deletions store/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
uploadURLToStorage,
deleteFileOnStorage,
} from '@/plugins/api-helpers'
export const strict = false

export const state = () => ({
myCollections: [],
Expand Down Expand Up @@ -80,7 +79,7 @@ export const actions = {
index = item.collections.indexOf(collection.params.titleBefore)
if (index !== -1) {
item.collections.splice(index, 1, collection.title)
await vuexContext.dispatch('cards/rewriteCard', item, { root: true })
vuexContext.dispatch('cards/rewriteCard', item, { root: true })
}
}
delete collection.params
Expand Down Expand Up @@ -165,8 +164,10 @@ export const actions = {
return collection
}
},
async test(vuexContext) {
await vuexContext.dispatch('auth/setLogoutTimer', 15000, { root: true })
async test(vuexContext, collection) {
const imageUploaded = await uploadURLToStorage.call(this, collection.image)
// eslint-disable-next-line
console.log(imageUploaded)
},
}

Expand Down

0 comments on commit a87ae26

Please sign in to comment.