Skip to content

Commit

Permalink
perf: 🚀 Support file manager upload to the current directory
Browse files Browse the repository at this point in the history
  • Loading branch information
viarotel committed Sep 7, 2024
1 parent f5206b5 commit 737c2a3
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 23 deletions.
22 changes: 14 additions & 8 deletions electron/exposes/adbkit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,21 @@ async function readdir(id, filePath) {
}

async function push(id, filePath, args = {}) {
const { progress, savePath = `/sdcard/Download/${path.basename(filePath)}` }
= args
const { progress, savePath = '/sdcard/Download' } = args

const transfer = await client.getDevice(id).push(filePath, savePath)
const fileName = path.basename(filePath)

const fullSavePath = `${savePath}/${fileName}`.replace(/\/+/g, '/')

const transfer = await client.getDevice(id).push(filePath, fullSavePath)

return new Promise((resolve, reject) => {
transfer.on('progress', (stats) => {
progress?.(stats)
})

transfer.on('end', () => {
resolve(savePath)
resolve(fullSavePath)
})

transfer.on('error', (err) => {
Expand All @@ -246,8 +249,11 @@ async function push(id, filePath, args = {}) {
}

async function pull(id, filePath, args = {}) {
const { progress, savePath = path.resolve('../', path.basename(filePath)) }
= args
const { progress, savePath = '../' } = args

const fileName = path.basename(filePath)

const fullSavePath = path.resolve(savePath, fileName)

const transfer = await client.getDevice(id).pull(filePath)

Expand All @@ -257,14 +263,14 @@ async function pull(id, filePath, args = {}) {
})

transfer.on('end', () => {
resolve(savePath)
resolve(fullSavePath)
})

transfer.on('error', (err) => {
reject(err)
})

transfer.pipe(fs.createWriteStream(savePath))
transfer.pipe(fs.createWriteStream(fullSavePath))
})
}

Expand Down
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default antfu(
'antfu/top-level-function': 'off',

'import/default': 'off',
'import/order': 'off',

'node/prefer-global/process': 'off',

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@
<script setup>
import { useDialog, useFileActions } from '$/composables/index.js'
import { usePreferenceStore } from '$/store'
import { ElMessageBox } from 'element-plus'
import AddPopover from './AddPopover/index.vue'
const preferenceStore = usePreferenceStore()
Expand Down Expand Up @@ -288,7 +286,10 @@ async function handleRemove(row) {
}
async function handleUpload() {
await fileActions.send(device.value)
await fileActions.send(device.value, {
savePath: currentPath.value,
})
getTableData()
}
Expand All @@ -312,18 +313,13 @@ async function handleDownload(row) {
.filter(item => item.type === 'file')
.map(item => item.id)
const deviceConfig = preferenceStore.getData(device.value.id)
const savePath = preferenceStore.getData(device.value.id)?.savePath
const closeLoading = ElMessage.loading(window.t('common.downloading')).close
for (let index = 0; index < pathList.length; index++) {
const item = pathList[index]
const savePath = window.nodePath.resolve(
deviceConfig.savePath,
window.nodePath.basename(item),
)
await window.adbkit
.pull(device.value.id, item, { savePath })
.catch(e => console.warn(e?.message))
Expand Down
4 changes: 2 additions & 2 deletions src/composables/useFileActions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function useFileActions() {
}
}

async function singleSend(device, { files, silent = false } = {}) {
async function singleSend(device, { savePath, files, silent = false } = {}) {
if (!files) {
try {
files = await selectFiles()
Expand All @@ -68,7 +68,7 @@ export function useFileActions() {
let failCount = 0

await allSettledWrapper(files, (item) => {
return window.adbkit.push(device.id, item).catch(() => {
return window.adbkit.push(device.id, item, { savePath }).catch(() => {
++failCount
})
})
Expand Down
2 changes: 1 addition & 1 deletion src/locales/languages/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"device.control.file.push.loading": "Push file...",
"device.control.file.push.success.name": "Push files successfully",
"device.control.file.push.success": "Successfully pushed {totalCount} files to {deviceName}, {successCount} succeeded, and {failCount} failed",
"device.control.file.push.success.single": "Files successfully pushed to the /sdcard/Download/ directory of {deviceName}",
"device.control.file.push.success.single": "Files successfully pushed to {deviceName}",
"device.control.file.push.error": "Failed to push the file, please check the file and try again",
"device.control.file.manager.storage": "Internal Storage",
"device.control.file.manager.add": "New Folder",
Expand Down
2 changes: 1 addition & 1 deletion src/locales/languages/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"device.control.file.push.loading": "推送文件中...",
"device.control.file.push.success.name": "推送文件成功",
"device.control.file.push.success": "已成功将 {totalCount} 个文件推送到 {deviceName},{successCount} 成功,{failCount} 失败。",
"device.control.file.push.success.single": "文件已成功推送到 {deviceName} 的 /sdcard/Download/ 目录",
"device.control.file.push.success.single": "文件已成功推送到 {deviceName}",
"device.control.file.push.error": "推送文件失败,请检查文件后重试",
"device.control.file.manager.storage": "内部存储空间",
"device.control.file.manager.add": "新建文件夹",
Expand Down
2 changes: 1 addition & 1 deletion src/locales/languages/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"device.control.file.push.loading": "推送檔案中...",
"device.control.file.push.success.name": "推送檔案成功",
"device.control.file.push.success": "已成功將 {totalCount} 個檔案推送到 {deviceName},{successCount} 成功,{failCount} 失敗。",
"device.control.file.push.success.single": "檔案已成功推送到 {deviceName} 的 /sdcard/Download/ 目錄",
"device.control.file.push.success.single": "檔案已成功推送到 {deviceName}",
"device.control.file.push.error": "推送檔案失敗,請檢查檔案後重試",
"device.control.file.manager.storage": "內部儲存空間",
"device.control.file.manager.add": "新增資料夾",
Expand Down
2 changes: 1 addition & 1 deletion src/store/preference/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { replaceIP, restoreIP } from '$/utils/index.js'
import { cloneDeep, get, pickBy, set } from 'lodash-es'

import { defineStore } from 'pinia'

import {
Expand All @@ -10,7 +11,6 @@ import {
mergeConfig,
setStoreData,
} from './helpers/index.js'

import model from './model/index.js'

const { adbPath, scrcpyPath, gnirehtetPath } = window.electron?.configs || {}
Expand Down

0 comments on commit 737c2a3

Please sign in to comment.