Skip to content

Commit f921354

Browse files
committed
fix(attachments): native file download doesn't work on Android
1 parent 8cf4e05 commit f921354

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

android/app/capacitor.build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ android {
99

1010
apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle"
1111
dependencies {
12-
12+
implementation project(':capacitor-community-file-opener')
13+
implementation project(':capacitor-filesystem')
1314

1415
}
1516

android/capacitor.settings.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
// DO NOT EDIT THIS FILE! IT IS GENERATED EACH TIME "capacitor update" IS RUN
22
include ':capacitor-android'
33
project(':capacitor-android').projectDir = new File('../node_modules/@capacitor/android/capacitor')
4+
5+
include ':capacitor-community-file-opener'
6+
project(':capacitor-community-file-opener').projectDir = new File('../node_modules/@capacitor-community/file-opener/android')
7+
8+
include ':capacitor-filesystem'
9+
project(':capacitor-filesystem').projectDir = new File('../node_modules/@capacitor/filesystem/android')

package-lock.json

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
"postinstall": "electron-builder install-app-deps && npm run schema:generate"
3636
},
3737
"dependencies": {
38+
"@capacitor-community/file-opener": "^6.0.1",
3839
"@capacitor/android": "^6.1.2",
3940
"@capacitor/core": "^6.1.2",
41+
"@capacitor/filesystem": "^6.0.3",
4042
"@emoji-mart/data": "^1.2.1",
4143
"@klayr/codec": "^0.5.1",
4244
"@klayr/cryptography": "^4.1.1",

src/components/AChat/AChatAttachment/AChatImageModal.vue

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ import { ref, computed, onMounted, PropType } from 'vue'
5858
import { useI18n } from 'vue-i18n'
5959
import { useStore } from 'vuex'
6060
import { breakpointsTailwind, useBreakpoints } from '@vueuse/core'
61+
import { Capacitor } from '@capacitor/core'
62+
import { Directory, Filesystem, WriteFileResult } from '@capacitor/filesystem'
63+
import { FileOpener } from '@capacitor-community/file-opener'
6164
6265
import AChatImageModalItem from './AChatImageModalItem.vue'
6366
import AChatModalFile from './AChatModalFile.vue'
@@ -69,7 +72,36 @@ function delay(ms: number) {
6972
return new Promise((resolve) => setTimeout(resolve, ms))
7073
}
7174
72-
function downloadFileByUrl(url: string, filename = 'unnamed') {
75+
async function downloadFileNatively(blobUrl: string, filename: string): Promise<WriteFileResult> {
76+
const response = await fetch(blobUrl)
77+
const blob = await response.blob()
78+
79+
return new Promise((resolve, reject) => {
80+
const reader = new FileReader()
81+
reader.readAsDataURL(blob)
82+
83+
reader.onloadend = async () => {
84+
const base64Data = reader.result?.toString().split(',')[1]
85+
86+
Filesystem.writeFile({
87+
path: filename,
88+
data: base64Data!,
89+
directory: Directory.Documents
90+
})
91+
.then(resolve)
92+
.catch(reject)
93+
}
94+
})
95+
}
96+
97+
async function downloadFileByUrl(url: string, filename = 'unnamed') {
98+
if (Capacitor.isNativePlatform()) {
99+
const fileResult = await downloadFileNatively(url, filename)
100+
await FileOpener.open({ filePath: fileResult.uri })
101+
102+
return
103+
}
104+
73105
const anchor = document.createElement('a')
74106
anchor.href = url
75107
anchor.download = filename
@@ -202,7 +234,7 @@ export default {
202234
const fileName = file.name
203235
? `${file.name}${file.extension ? '.' + file.extension : ''}`
204236
: undefined
205-
downloadFileByUrl(imageUrl, fileName)
237+
await downloadFileByUrl(imageUrl, fileName)
206238
} catch {
207239
void store.dispatch('snackbar/show', {
208240
message: t('chats.file_not_found')

0 commit comments

Comments
 (0)