Support uploading local files

This commit is contained in:
Trevor Slocum 2020-09-08 15:54:10 -07:00
parent cd13013cc4
commit 5ff1d4b36b
3 changed files with 68 additions and 3 deletions

View File

@ -10,4 +10,4 @@ ADB file manager
## Support
Please share issues/suggestions [here](https://gitlab.com/tslocum/adbfm/issues).
Please share issues and suggestions [here](https://gitlab.com/tslocum/adbfm/issues).

View File

@ -2,6 +2,7 @@ package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
@ -66,7 +67,37 @@ func initTUI() error {
localBuffer.SetTitleAlign(cview.AlignLeft).SetBorder(true)
localBuffer.
AddContextItem("Upload", 'z', func(index int) {
// TODO Exists confirmation dialog
modalPopup.SetText(fmt.Sprintf("Uploading %s", localEntriesShown[index].Name))
app.SetRoot(appContainer, true)
focusUpdated()
app.ForceDraw()
f, err := os.Open(path.Join(localPath, localEntriesShown[index].Name))
if err != nil {
log.Fatalf("failed to open local file: %s", err)
}
data, err := ioutil.ReadAll(f)
if err != nil {
log.Fatalf("failed to read local file: %s", err)
}
stat, err := f.Stat()
if err != nil {
log.Fatalf("failed to fetch info of local file: %s", err)
}
err = bridge.Upload(localEntriesShown[index].Name, data, stat.ModTime())
if err != nil {
log.Fatalf("failed to upload file: %s", err)
}
browseRemote(remotePath)
app.SetRoot(appGrid, true)
focusUpdated()
}).
AddContextItem("", 0, nil).
AddContextItem("Cut", 'x', func(index int) {
@ -290,6 +321,13 @@ func browseRemote(p string) {
remoteLock.Lock()
defer remoteLock.Unlock()
previousSelection := -1
previousOffset := -1
if remotePath == p {
previousSelection = remoteBuffer.GetCurrentItem()
previousOffset = remoteBuffer.GetOffset()
}
oldPath := remotePath
app.QueueUpdateDraw(func() {
remoteBuffer.SetTitle(" " + oldPath + "... ")
@ -323,5 +361,10 @@ func browseRemote(p string) {
remoteBuffer.AddItem(label, "", 0, nil)
remoteEntriesShown = append(remoteEntriesShown, entry)
}
if previousSelection >= 0 {
remoteBuffer.SetCurrentItem(previousSelection)
remoteBuffer.SetOffset(previousOffset)
}
})
}

View File

@ -10,6 +10,7 @@ import (
"sort"
"strings"
"sync"
"time"
adb "github.com/zach-klippenstein/goadb"
)
@ -82,14 +83,35 @@ func (a *Bridge) setDevice(deviceInfo *adb.DeviceInfo) (err error) {
return nil
}
func (a *Bridge) Download(downloadPath string) ([]byte, error) {
func (a *Bridge) Upload(fileName string, data []byte, mtime time.Time) error {
a.Lock()
defer a.Unlock()
reader, err := a.device.OpenRead(path.Join(a.dir, downloadPath))
writer, err := a.device.OpenWrite(path.Join(a.dir, fileName), 0655, mtime)
if err != nil {
return fmt.Errorf("failed to open remote file: %s", err)
}
defer writer.Close()
n, err := writer.Write(data)
if err != nil {
return err
} else if n != len(data) {
return errors.New("failed to write file")
}
return nil
}
func (a *Bridge) Download(fileName string) ([]byte, error) {
a.Lock()
defer a.Unlock()
reader, err := a.device.OpenRead(path.Join(a.dir, fileName))
if err != nil {
return nil, err
}
defer reader.Close()
data, err := ioutil.ReadAll(reader)
if err != nil {