Skip to content

Commit

Permalink
fix: Download attachment.
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwinBetanc0urt committed Dec 28, 2023
1 parent 5420298 commit 2066686
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 154 deletions.
45 changes: 45 additions & 0 deletions src/api/ADempiere/file-management/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* ADempiere-Vue (Frontend) for ADempiere ERP & CRM Smart Business Solution
* Copyright (C) 2018-Present E.R.P. Consultores y Asociados, C.A. www.erpya.com
* Contributor(s): Elsio Sanchez [email protected] https://github.com/elsiosanchez
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

// Get Instance for connection
import { request } from '@/utils/ADempiere/request'

// Utils and Helper Methods
import { isEmptyValue } from '@/utils/ADempiere/valueUtils'

/**
* Get resource
* @param {number} id
* @param {string} resourceName as fileName
* @returns {promise}
*/
export function requestGetResource({
id,
//
resourceName
}) {
let path = `/file-management/resources/${id}`
if (isEmptyValue(resourceName)) {
path = `/file-management/resources/file-name/${resourceName}`
}

return request({
url: path,
method: 'get'
})
}
31 changes: 0 additions & 31 deletions src/api/ADempiere/user-interface/component/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,6 @@ import { request } from '@/utils/ADempiere/request'
// Constants
import { config } from '@/utils/ADempiere/config'

// Download a resource from file name
export function requestResource({ resourceUuid, resourceName }, callBack = {
onData: () => {},
onStatus: () => {},
onEnd: () => {}
}) {
const { getResoursePath } = require('@/utils/ADempiere/resource.js')
const { urn } = getResoursePath({
resourceUuid,
resourceName
})
return request({
url: urn,
method: 'get',
// responseType: 'arraybuffer',
baseURL: config.adempiere.resource.url
})
}

export function requestDownloadResource({ resourceUuid, resourceName }) {
return request({
url: '/user-interface/component/resource/download',
method: 'get',
// responseType: 'arraybuffer',
params: {
resource_uuid: resourceUuid,
resource_name: resourceName
}
})
}

/**
* Get image with uri request
* @param {string} file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ import store from '@/store'

// API Request Methods
import {
requestResource,
sendAttachmentDescription,
sendAttachmentDescriptionHeader
} from '@/api/ADempiere/user-interface/component/resource'
import {
requestDeleteResourceReference
} from '@/api/ADempiere/file-management/resource-reference.ts'
import {
requestGetResource
} from '@/api/ADempiere/file-management/resources.ts'

// Components and Mixins
import FileRender from '@/components/ADempiere/FileRender/index.vue'
import ListView from './listView.vue'
import LoadingView from '@/components/ADempiere/LoadingView/index.vue'
import UploadResource from './uploadResource.vue'
import PanelFooter from '@/components/ADempiere/PanelFooter/index.vue'
Expand All @@ -42,7 +45,6 @@ import { showMessage } from '@/utils/ADempiere/notification.js'
import {
buildLinkHref,
formatFileSize,
getImagePath,
getImageFromContentType
} from '@/utils/ADempiere/resource.js'

Expand All @@ -51,6 +53,7 @@ export default defineComponent({

components: {
FileRender,
ListView,
LoadingView,
PanelFooter,
UploadResource
Expand Down Expand Up @@ -115,9 +118,10 @@ export default defineComponent({
return []
}
return storedResourcesList.map(element => {
const sourceFile = getSurceFile(element)
return {
...element,
src: getSurceFile(element),
src: sourceFile,
isShowMessage: false
}
})
Expand Down Expand Up @@ -178,21 +182,20 @@ export default defineComponent({
* @param {Boolean} isDownload
*/
const handleDownload = async(file, isDownload = true) => {
let link
if (file.content_type.includes('image')) {
const imagen = await fetch(file.src)
const imagenblob = await imagen.blob()
const imageURL = URL.createObjectURL(imagenblob)
link = document.createElement('a')
link.href = imageURL
link.download = file.name
link.click()
return
}

requestResource({
resourceUuid: file.uuid,
resourceName: file.file_name
// let link
// if (file.content_type.includes('image')) {
// const imagen = await fetch(file.src)
// const imagenblob = await imagen.blob()
// const imageURL = URL.createObjectURL(imagenblob)
// link = document.createElement('a')
// link.href = imageURL
// link.download = file.name
// link.click()
// return
// }
requestGetResource({
id: file.id,
resourceName: file.valid_file_name
}).then(response => {
buildLinkHref({
fileName: file.name,
Expand All @@ -209,7 +212,7 @@ export default defineComponent({
*/
function getSurceFile(file) {
if (file.content_type.includes('image')) {
return getImageFromSource(file).uri
return getImageFromSource(file)
}
return getImageFromContentType({
contentType: file.content_type,
Expand All @@ -221,13 +224,16 @@ export default defineComponent({
* Image From Source
* @param {Object} file
*/
const getImageFromSource = (file) => {
const image = getImagePath({
file: file.file_name,
width: 900,
height: 500
const getImageFromSource = async(file) => {
const bytes = await requestGetResource({
id: file.id,
resourceName: file.valid_file_name
})
return image

const base64_array = bytes.map(part => part.data)
const base64_string = base64_array.join('')

return 'data:' + file.content_type + ';base64,' + base64_string
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<!--
ADempiere-Vue (Frontend) for ADempiere ERP & CRM Smart Business Solution
Copyright (C) 2018-Present E.R.P. Consultores y Asociados, C.A. www.erpya.com
Contributor(s): Elsio Sanchez elsiosanches@gmail.com https://github.com/elsiosanchez
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https:www.gnu.org/licenses/>.
-->

<template>
<el-row :gutter="20">
<el-col v-for="(file, key) in attachmentList" :key="key" :span="24">
<el-card shadow="hover" :class="{ 'image-attachment': true, 'is-current': isCurrent(file) }">
<div style="float: left;width: 40%; height: 100px;">
<el-image
class="image-card-attachment"
:src="file.src"
fit="contain"
:preview-src-list="previewList"
style="padding-left: 0px;padding-right: 0px;border: 1px solid rgba(184, 186, 188, 0.64);width: 150px;height: 100px;float: left;margin-top: 5%;"
/>
</div>
<div style="float: left;padding-top: 2%;width: 60%;">
<el-radio
v-if="isSelectable"
v-model="resourceReference.id"
:label="file.id"
@click="resourceReference = file"
>
{{ file.name }}
</el-radio>
<p
v-else
style="font-size: 14px;box-sizing: border-box;overflow: hidden;text-overflow: ellipsis;white-space: normal;word-break: break-all;text-align: end;padding-right: 10px;"
>
{{ file.name }}
<br>
{{ formatFileSize(file.file_size) }}
</p>
<!-- <b>{{ file.name }}</b> -->
<br>
<!-- <span>{{ formatFileSize(file.file_size) }}</span> -->
</div>
</el-card>
</el-col>
</el-row>
</template>

<script>
import { defineComponent, computed } from '@vue/composition-api'

import store from '@/store'

// Utils and Helper Methods
import { isEmptyValue } from '@/utils/ADempiere/valueUtils'
import {
formatFileSize,
getImagePath,
getImageFromContentType
} from '@/utils/ADempiere/resource.js'

export default defineComponent({
name: 'ListView',

props: {
isSelectable: {
type: Boolean,
default: false
}
},

setup(props) {
const attachmentList = computed({
set(value) {
store.commit('setListAttachment', value)
},
get() {
const storedResourcesList = store.getters.getListAttachment
if (isEmptyValue(storedResourcesList)) {
return []
}
return storedResourcesList.map(element => {
return {
...element,
src: getSurceFile(element),
isShowMessage: false
}
})
}
})

const previewList = computed(() => {
return attachmentList.value.map(file => file.src)
})

const resourceReference = computed({
set(value) {
store.commit('setResourceReference', value)
},
get() {
return store.getters.getResourceReference
}
})

/**
* Current Filed
* @param {Object} file
*/
function isCurrent(file) {
if (!props.isSelectable) {
return false
}
if (isEmptyValue(resourceReference.value)) {
return false
}
return resourceReference.value.id === file.id
}

/**
* Image From Source
* @param {Object} file
*/
const getImageFromSource = (file) => {
const image = getImagePath({
file: file.file_name,
width: 900,
height: 500
})
return image
}

/**
* Get Surce File
* @param {Object} file
*/
function getSurceFile(file) {
if (file.content_type.includes('image')) {
return getImageFromSource(file).uri
}
return getImageFromContentType({
contentType: file.content_type,
fileName: file.file_name
})
}

return {
// computeds
attachmentList,
previewList,
// methods
formatFileSize,
isCurrent
}
}
})
</script>
Loading

0 comments on commit 2066686

Please sign in to comment.