mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-18 00:02:15 +00:00
303 lines
9.4 KiB
JavaScript
Vendored
303 lines
9.4 KiB
JavaScript
Vendored
import store from './store/index'
|
|
import {debounce, includes} from "lodash";
|
|
import {events} from './bus'
|
|
import axios from 'axios'
|
|
import router from '@/router'
|
|
|
|
const Helpers = {
|
|
install(Vue) {
|
|
|
|
Vue.prototype.$updateText = debounce(function (route, name, value) {
|
|
|
|
if (value === '') return
|
|
|
|
axios.patch(this.$store.getters.api + route, {name, value})
|
|
.catch(error => {
|
|
events.$emit('alert:open', {
|
|
title: this.$t('popup_error.title'),
|
|
message: this.$t('popup_error.message'),
|
|
})
|
|
})
|
|
}, 150)
|
|
|
|
Vue.prototype.$updateImage = function (route, name, image) {
|
|
|
|
// Create form
|
|
let formData = new FormData()
|
|
|
|
// Add image to form
|
|
formData.append('name', name)
|
|
formData.append(name, image)
|
|
formData.append('_method', 'PATCH')
|
|
|
|
axios.post(this.$store.getters.api + route, formData, {
|
|
headers: {
|
|
'Content-Type': 'multipart/form-data',
|
|
}
|
|
})
|
|
.catch(error => {
|
|
events.$emit('alert:open', {
|
|
title: this.$t('popup_error.title'),
|
|
message: this.$t('popup_error.message'),
|
|
})
|
|
})
|
|
}
|
|
|
|
Vue.prototype.$scrollTop = function () {
|
|
var container = document.getElementById('vue-file-manager')
|
|
|
|
if (container) {
|
|
container.scrollTop = 0
|
|
}
|
|
}
|
|
|
|
Vue.prototype.$getImage = function (source) {
|
|
return source ? '/' + source : ''
|
|
}
|
|
|
|
Vue.prototype.$getCreditCardBrand = function (brand) {
|
|
return `/assets/icons/${brand}.svg`
|
|
}
|
|
|
|
Vue.prototype.$getInvoiceLink = function (customer, id) {
|
|
return '/invoice/' + customer + '/' + id
|
|
}
|
|
|
|
Vue.prototype.$openImageOnNewTab = function (source) {
|
|
let win = window.open(source, '_blank')
|
|
|
|
win.focus()
|
|
}
|
|
|
|
Vue.prototype.$createFolder = function (folderName) {
|
|
this.$store.dispatch('createFolder', folderName)
|
|
}
|
|
|
|
Vue.prototype.$uploadFiles = async function (files) {
|
|
// Prevent submit empty files
|
|
if (files && files.length == 0) return
|
|
|
|
let fileCount = files ? files.length : 0
|
|
let fileCountSucceed = 1
|
|
|
|
store.commit('UPDATE_FILE_COUNT_PROGRESS', {
|
|
current: fileCountSucceed,
|
|
total: fileCount
|
|
})
|
|
|
|
store.commit('UPLOADING_FILE_PROGRESS', 0)
|
|
|
|
// Get parent id
|
|
const rootFolder = this.$store.getters.currentFolder
|
|
? this.$store.getters.currentFolder.unique_id
|
|
: 0
|
|
|
|
for (var i = files.length - 1; i >= 0; i--) {
|
|
|
|
let file = files[i],
|
|
chunks = []
|
|
|
|
// Calculate ceils
|
|
let size = this.$store.getters.config.chunkSize,
|
|
chunksCeil = Math.ceil(file.size / size);
|
|
|
|
// Create chunks
|
|
for (let i = 0; i < chunksCeil; i++) {
|
|
chunks.push(file.slice(
|
|
i * size, Math.min(i * size + size, file.size), file.type
|
|
));
|
|
}
|
|
|
|
// Set form Data
|
|
let formData = new FormData()
|
|
let uploadedSize = 0
|
|
|
|
do {
|
|
let isLast = chunks.length === 1
|
|
let chunk = chunks.shift()
|
|
let attempts = 0
|
|
|
|
// Set form data
|
|
formData.set('is_last', isLast);
|
|
formData.set('file', chunk, `${file.name}.part`);
|
|
formData.set('parent_id', rootFolder)
|
|
|
|
// Upload data
|
|
do {
|
|
await store.dispatch('uploadFiles', {
|
|
form: formData,
|
|
fileSize: file.size,
|
|
totalUploadedSize: uploadedSize
|
|
}).then(() => {
|
|
uploadedSize = uploadedSize + chunk.size
|
|
}).catch(() => {
|
|
|
|
attempts++
|
|
|
|
if (attempts === 3) {
|
|
events.$emit('alert:open', {
|
|
title: this.$t('popup_error.title'),
|
|
message: this.$t('popup_error.message'),
|
|
})
|
|
}
|
|
})
|
|
} while (attempts !== 0 && attempts !== 3)
|
|
|
|
} while (chunks.length !== 0)
|
|
|
|
// Progress file log
|
|
store.commit('UPDATE_FILE_COUNT_PROGRESS', {
|
|
current: fileCountSucceed,
|
|
total: fileCount
|
|
})
|
|
|
|
// Uploading finished
|
|
if (fileCount === fileCountSucceed) {
|
|
store.commit('UPDATE_FILE_COUNT_PROGRESS', undefined)
|
|
} else {
|
|
// Add uploaded file
|
|
fileCountSucceed++
|
|
}
|
|
}
|
|
}
|
|
|
|
Vue.prototype.$uploadExternalFiles = async function (event, parent_id) {
|
|
|
|
// Prevent submit empty files
|
|
if (event.dataTransfer.items.length == 0) return
|
|
|
|
// Get files
|
|
const files = [...event.dataTransfer.items].map(item => item.getAsFile());
|
|
|
|
let fileCountSucceed = 1
|
|
|
|
store.commit('UPDATE_FILE_COUNT_PROGRESS', {
|
|
current: fileCountSucceed,
|
|
total: files.length
|
|
})
|
|
|
|
for (var i = files.length - 1; i >= 0; i--) {
|
|
|
|
let formData = new FormData()
|
|
|
|
// Append data
|
|
formData.append('file', files[i])
|
|
|
|
// Append form data
|
|
formData.append('parent_id', parent_id)
|
|
|
|
// Upload data
|
|
await store.dispatch('uploadFiles', formData).then(() => {
|
|
// Progress file log
|
|
store.commit('UPDATE_FILE_COUNT_PROGRESS', {
|
|
current: fileCountSucceed,
|
|
total: files.length
|
|
})
|
|
// Progress file log
|
|
store.commit('INCREASE_FOLDER_ITEM', parent_id)
|
|
|
|
// Uploading finished
|
|
if (files.length === fileCountSucceed) {
|
|
store.commit('UPDATE_FILE_COUNT_PROGRESS', undefined)
|
|
} else {
|
|
// Add uploaded file
|
|
fileCountSucceed++
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
Vue.prototype.$downloadFile = function (url, filename) {
|
|
var anchor = document.createElement('a')
|
|
|
|
anchor.href = url
|
|
|
|
anchor.download = filename
|
|
|
|
document.body.appendChild(anchor)
|
|
|
|
anchor.click()
|
|
}
|
|
|
|
Vue.prototype.$closePopup = function () {
|
|
events.$emit('popup:close')
|
|
}
|
|
|
|
Vue.prototype.$isThisRoute = function (route, locations) {
|
|
|
|
return includes(locations, route.name)
|
|
}
|
|
|
|
Vue.prototype.$isThisLocation = function (location) {
|
|
|
|
// Get current location
|
|
let currentLocation = store.getters.currentFolder && store.getters.currentFolder.location ? store.getters.currentFolder.location : undefined
|
|
|
|
// Check if type is object
|
|
if (typeof location === 'Object' || location instanceof Object) {
|
|
return includes(location, currentLocation)
|
|
|
|
} else {
|
|
return currentLocation === location
|
|
}
|
|
}
|
|
|
|
Vue.prototype.$checkPermission = function (type) {
|
|
|
|
let currentPermission = store.getters.permission
|
|
|
|
// Check if type is object
|
|
if (typeof type === 'Object' || type instanceof Object) {
|
|
return includes(type, currentPermission)
|
|
|
|
} else {
|
|
return currentPermission === type
|
|
}
|
|
}
|
|
|
|
Vue.prototype.$isMobile = function () {
|
|
const toMatch = [
|
|
/Android/i,
|
|
/webOS/i,
|
|
/iPhone/i,
|
|
/iPad/i,
|
|
/iPod/i,
|
|
/BlackBerry/i,
|
|
/Windows Phone/i
|
|
]
|
|
|
|
return toMatch.some(toMatchItem => {
|
|
return navigator.userAgent.match(toMatchItem)
|
|
})
|
|
}
|
|
|
|
Vue.prototype.$isMinimalScale = function () {
|
|
let sizeType = store.getters.filesViewWidth
|
|
|
|
return sizeType === 'minimal-scale'
|
|
}
|
|
|
|
Vue.prototype.$isCompactScale = function () {
|
|
let sizeType = store.getters.filesViewWidth
|
|
|
|
return sizeType === 'compact-scale'
|
|
}
|
|
|
|
Vue.prototype.$isFullScale = function () {
|
|
let sizeType = store.getters.filesViewWidth
|
|
|
|
return sizeType === 'full-scale'
|
|
}
|
|
|
|
Vue.prototype.$isSomethingWrong = function () {
|
|
|
|
events.$emit('alert:open', {
|
|
title: this.$t('popup_error.title'),
|
|
message: this.$t('popup_error.message'),
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
export default Helpers
|