fileIo.js
7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { isRestrictedInternalPath } from './pathAccess.js'
import { UNIAPP_SAVE_DIR } from './constants.js'
import { getRuntimePackageName } from './packageName.js'
import {
copyFileByAndroid,
fileExistsByAndroid,
isInternalSandboxAbsPath,
resolvePlusAbsolutePath
} from './androidFileCopy.js'
import { logStep, logStepFail } from './log.js'
const STAGE = '归并·文件'
function toFileUrl(path) {
if (!path) return path
const p = String(path).trim()
if (p.startsWith('file://')) return p
if (p.startsWith('_')) return p
if (p.startsWith('/')) return `file://${p}`
return `file:///${p}`
}
export async function fileExists(path) {
if (!path || typeof plus === 'undefined') return false
const p = String(path)
.trim()
.replace(/^file:\/\//, '')
if (isInternalSandboxAbsPath(p) && fileExistsByAndroid(p)) {
return true
}
return new Promise(resolve => {
plus.io.resolveLocalFileSystemURL(
toFileUrl(path),
() => resolve(true),
() => resolve(false)
)
})
}
function copyEntryToDocDir(srcEntry, docRelativeDir, destFileName) {
const docDir = docRelativeDir.endsWith('/') ? docRelativeDir : `${docRelativeDir}/`
return new Promise((resolve, reject) => {
plus.io.resolveLocalFileSystemURL(
docDir.startsWith('_doc') ? docDir : `_doc/${docDir}`,
dirEntry => {
srcEntry.copyTo(
dirEntry,
destFileName,
entry => resolve(entry.fullPath),
err => reject(new Error(err?.message || JSON.stringify(err)))
)
},
err => reject(new Error(err?.message || JSON.stringify(err)))
)
})
}
function copyFileToDocByPlusIo(sourceAbsolutePath, destFileName) {
return new Promise((resolve, reject) => {
plus.io.resolveLocalFileSystemURL(
toFileUrl(sourceAbsolutePath),
srcEntry => {
copyEntryToDocDir(srcEntry, '_doc/', destFileName).then(resolve).catch(reject)
},
err => reject(new Error(err?.message || JSON.stringify(err)))
)
})
}
export async function copyFileToDoc(sourceAbsolutePath, destFileName) {
const pkg = getRuntimePackageName()
if (isRestrictedInternalPath(sourceAbsolutePath, pkg)) {
const err = new Error('无法访问非本应用的内部路径,仅合并当前应用沙箱内的库文件')
logStepFail(STAGE, '拒绝复制:非本应用内部路径', { 来源: sourceAbsolutePath })
return Promise.reject(err)
}
const src = String(sourceAbsolutePath).trim()
const destRel = destFileName.startsWith('_doc/') ? destFileName : `_doc/${destFileName}`
const destAbs = resolvePlusAbsolutePath(destRel)
if (isInternalSandboxAbsPath(src)) {
try {
const out = await copyFileByAndroid(src, destAbs)
return out
} catch (e) {
logStepFail(STAGE, 'File API 复制失败', { 来源: src, 错误: e?.message || String(e) })
throw e
}
}
try {
const out = await copyFileToDocByPlusIo(src, destFileName)
return out
} catch (e) {
const msg = e?.message || String(e)
if (isInternalSandboxAbsPath(src) || /36199|不支持当前路径|targetSdk/i.test(msg)) {
try {
const out = await copyFileByAndroid(src, destAbs)
return out
} catch (e2) {
logStepFail(STAGE, '降级 File API 仍失败', { 错误: e2?.message || String(e2) })
throw e2
}
}
logStepFail(STAGE, 'plus.io 复制失败', { 来源: src, 错误: msg })
throw e
}
}
/** 复制到 _doc/uniapp_save/(目录不存在时由 plus 创建父级需先 ensure) */
async function copyIntoDocSaveDirByPlusIo(sourceAbsolutePath, destFileName) {
return new Promise((resolve, reject) => {
const ensureThenCopy = () => {
plus.io.resolveLocalFileSystemURL(
toFileUrl(sourceAbsolutePath),
srcEntry => {
plus.io.resolveLocalFileSystemURL(
`_doc/${UNIAPP_SAVE_DIR}/`,
dirEntry => {
srcEntry.copyTo(
dirEntry,
destFileName,
entry => resolve(entry.fullPath),
err => reject(new Error(err?.message || JSON.stringify(err)))
)
},
() => {
plus.io.resolveLocalFileSystemURL(
'_doc/',
docEntry => {
docEntry.getDirectory(
UNIAPP_SAVE_DIR,
{ create: true },
saveDir => {
plus.io.resolveLocalFileSystemURL(
toFileUrl(sourceAbsolutePath),
src2 =>
src2
.copyTo(
saveDir,
destFileName,
entry => resolve(entry.fullPath),
e => reject(new Error(e?.message || JSON.stringify(e)))
),
e => reject(new Error(e?.message || JSON.stringify(e)))
)
},
e => reject(new Error(e?.message || JSON.stringify(e)))
)
},
e => reject(new Error(e?.message || JSON.stringify(e)))
)
}
)
},
err => reject(new Error(err?.message || JSON.stringify(err)))
)
}
ensureThenCopy()
})
}
export async function copyFileIntoDocSaveDir(sourceAbsolutePath, destFileName) {
const pkg = getRuntimePackageName()
if (isRestrictedInternalPath(sourceAbsolutePath, pkg)) {
logStepFail(STAGE, '拒绝复制录音:非本应用内部路径', { 来源: sourceAbsolutePath })
return Promise.reject(new Error('无法从非本应用内部路径复制录音文件'))
}
const src = String(sourceAbsolutePath).trim()
const destAbs = resolvePlusAbsolutePath(`_doc/${UNIAPP_SAVE_DIR}/${destFileName}`)
if (isInternalSandboxAbsPath(src)) {
try {
const out = await copyFileByAndroid(src, destAbs)
return out
} catch (e) {
logStepFail(STAGE, '录音 File API 复制失败', { 错误: e?.message || String(e) })
throw e
}
}
try {
const out = await copyIntoDocSaveDirByPlusIo(src, destFileName)
return out
} catch (e) {
const msg = e?.message || String(e)
if (/36199|不支持当前路径|targetSdk/i.test(msg)) {
try {
const out = await copyFileByAndroid(src, destAbs)
return out
} catch (e2) {
logStepFail(STAGE, '录音降级复制失败', { 错误: e2?.message || String(e2) })
throw e2
}
}
logStepFail(STAGE, '录音 plus.io 复制失败', { 错误: msg })
throw e
}
}
export function removeFile(path) {
logStep(STAGE, '尝试删除文件', { 路径: path })
return new Promise((resolve, reject) => {
plus.io.resolveLocalFileSystemURL(
toFileUrl(path),
entry => {
entry.remove(
() => {
logStep(STAGE, '文件删除成功', { 路径: path })
resolve(true)
},
err => {
const msg = err?.message || JSON.stringify(err)
logStepFail(STAGE, '文件删除失败', { 路径: path, 错误: msg })
reject(new Error(msg))
}
)
},
() => {
logStep(STAGE, '删除跳过:路径无法解析', { 路径: path })
resolve(false)
}
)
})
}
export function removeDocRelative(relativePath) {
const path = relativePath.startsWith('_doc/') ? relativePath : `_doc/${relativePath}`
return removeFile(path).catch(() => false)
}