Skip to content

Commit

Permalink
Merge pull request #55 from MHNightCat/extract-compress
Browse files Browse the repository at this point in the history
Update unzip function
  • Loading branch information
yorukot authored Apr 11, 2024
2 parents 90ac2c9 + aab0044 commit bc8cbd7
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 61 deletions.
116 changes: 116 additions & 0 deletions src/components/function.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package components

import (
"archive/zip"
"encoding/json"
"fmt"
"io"
Expand All @@ -15,6 +16,7 @@ import (
"time"

"github.com/barasher/go-exiftool"
"github.com/charmbracelet/bubbles/progress"
"github.com/lithammer/shortuuid"
"github.com/rkoesters/xdg/userdirs"
"github.com/shirou/gopsutil/disk"
Expand Down Expand Up @@ -521,3 +523,117 @@ func loadConfigFile(dir string) (toggleDotFileBool bool, firstFilePanelDir strin
}
return toggleDotFileBool, firstFilePanelDir
}

func unzip(src, dest string) error {
id := shortuuid.New()
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer func() {
if err := r.Close(); err != nil {
panic(err)
}
}()
totalFile := len(r.File)
// progessbar
prog := progress.New(progress.WithScaledGradient(theme.ProcessBarGradient[0], theme.ProcessBarGradient[1]))
prog.PercentageStyle = textStyle
// channel message
p := process{
name: "test",
progress: prog,
state: inOperation,
total: totalFile,
done: 0,
}
if _, err := os.Stat(filepath.Join(dest, filepath.Base(src))); os.IsExist(err) {
p.state = failure
p.name = "󰛫 Directory already exist"
channel <- channelMessage{
messageId: id,
processNewState: p,
}
return nil
}
os.MkdirAll(dest, 0755)

// Closure to address file descriptors issue with all the deferred .Close() methods
extractAndWriteFile := func(f *zip.File) error {

rc, err := f.Open()
if err != nil {
return err
}
defer func() {
if err := rc.Close(); err != nil {
panic(err)
}
}()

path := filepath.Join(dest, f.Name)

// Check for ZipSlip (Directory traversal)
if !strings.HasPrefix(path, filepath.Clean(dest)+string(os.PathSeparator)) {
return fmt.Errorf("illegal file path: %s", path)
}

if f.FileInfo().IsDir() {
os.MkdirAll(path, f.Mode())
} else {
os.MkdirAll(filepath.Dir(path), f.Mode())
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()

_, err = io.Copy(f, rc)

if err != nil {

return err
}
}
return nil
}

for _, f := range r.File {
p.name = "󰛫 " + f.Name
if len(channel) < 3 {
channel <- channelMessage{
messageId: id,
processNewState: p,
}
}
err := extractAndWriteFile(f)
if err != nil {
p.state = failure
channel <- channelMessage{
messageId: id,
processNewState: p,
}
return err
}
p.done++
if len(channel) < 3 {
channel <- channelMessage{
messageId: id,
processNewState: p,
}
}
}

p.state = successful
p.total = totalFile
channel <- channelMessage{
messageId: id,
processNewState: p,
}

return nil
}
7 changes: 7 additions & 0 deletions src/components/globalController.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,10 @@ func toggleDotFileController(m model) model {
}
return m
}

func extractFile(m model) model {
panel := m.fileModel.filePanels[m.filePanelFocusIndex]
unzip(panel.element[panel.cursor].location, filepath.Dir(panel.element[panel.cursor].location))
m.fileModel.filePanels[m.filePanelFocusIndex] = panel
return m
}
6 changes: 5 additions & 1 deletion src/components/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var channel = make(chan channelMessage, 1000)

func InitialModel(dir string) model {
toggleDotFileBool, firstFilePanelDir := loadConfigFile(dir)

return model{
filePanelFocusIndex: 0,
focusPanel: nonePanelFocus,
Expand Down Expand Up @@ -219,10 +220,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case Config.FilePanelDirectoryCreate[0], Config.FilePanelDirectoryCreate[1]:
m = panelCreateNewFolder(m)
case Config.PinnedDirectory[0], Config.PinnedDirectory[1]:
outPutLog("test")
m = pinnedFolder(m)
case Config.ToggleDotFile[0], Config.ToggleDotFile[1]:
m = toggleDotFileController(m)
case Config.ExtractFile[0], Config.ExtractFile[1]:
go func() {
m = extractFile(m)
}()
default:
// check if it's the select mode
if m.fileModel.filePanels[m.filePanelFocusIndex].focusType == focus && m.fileModel.filePanels[m.filePanelFocusIndex].panelMode == selectMode {
Expand Down
5 changes: 4 additions & 1 deletion src/components/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ type ConfigType struct {
FilePanelFileCreate [2]string
FilePanelItemRename [2]string
PasteItem [2]string
ToggleDotFile [2]string
ExtractFile [2]string
CompressFile [2]string

ToggleDotFile [2]string

Cancel [2]string
Confirm [2]string
Expand Down
119 changes: 61 additions & 58 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,61 +319,64 @@ func Unzip(src, dest string) error {
return nil
}

const configJsonString string = `{
"theme": "gruvbox",
"terminal": "",
"terminalWorkDirFlag": "",
"_COMMIT_bottom_panel": "This is currently of no use",
"bottomPanelList": ["processes", "metadata", "clipboard"],
"_COMMIT_HOTKEY": "",
"_COMMIT_global_hotkey": "Here is global, all global key cant conflicts with other hotkeys",
"reload": ["ctrl+r", ""],
"quit": ["esc", "q"],
"listUp": ["up", "k"],
"listDown": ["down", "j"],
"pinnedDirectory": ["ctrl+p", ""],
"closeFilePanel": ["ctrl+w", ""],
"createNewFilePanel": ["ctrl+n", ""],
"nextFilePanel": ["tab", ""],
"previousFilePanel": ["shift+left", ""],
"focusOnProcessBar": ["p", ""],
"focusOnSideBar": ["b", ""],
"focusOnMetaData": ["m", ""],
"changePanelMode": ["v", ""],
"filePanelFolderCreate": ["f", ""],
"filePanelFileCreate": ["c", ""],
"filePanelItemRename": ["r", ""],
"pasteItem": ["ctrl+v", ""],
"toggleDotFile": ["ctrl+h", ""],
"_COMMIT_special_hotkey": "These hotkeys do not conflict with any other keys (including global hotkey)",
"cancel": ["ctrl+c", "esc"],
"confirm": ["enter", ""],
"_COMMIT_normal_mode_hotkey": "Here is normal mode hotkey you can conflicts with other mode (cant conflicts with global hotkey)",
"deleteItem": ["ctrl+d", ""],
"selectItem": ["enter", "l"],
"parentFolder": ["h", "backspace"],
"copySingleItem": ["ctrl+c", ""],
"cutSingleItem": ["ctrl+x", ""],
"_COMMIT_select_mode_hotkey": "Here is select mode hotkey you can conflicts with other mode (cant conflicts with global hotkey)",
"filePanelSelectModeItemSingleSelect": ["enter", "l"],
"filePanelSelectModeItemSelectDown": ["shift+down", "J"],
"filePanelSelectModeItemSelectUp": ["shift+up", "K"],
"filePanelSelectModeItemDelete": ["ctrl+d", "delete"],
"filePanelSelectModeItemCopy": ["ctrl+c", ""],
"filePanelSelectModeItemCut": ["ctrl+x", ""],
"filePanelSelectAllItem": ["ctrl+a", ""],
"_COMMIT_process_bar_hotkey": "Here is process bar panel hotkey you can conflicts with other mode (cant conflicts global hotkey)"
}`
const configJsonString string =`{
"theme": "gruvbox",
"terminal": "",
"terminalWorkDirFlag": "",
"_COMMIT_bottom_panel": "This is currently of no use",
"bottomPanelList": ["processes", "metadata", "clipboard"],
"_COMMIT_HOTKEY": "",
"_COMMIT_global_hotkey": "Here is global, all global key cant conflicts with other hotkeys",
"reload": ["ctrl+r", ""],
"quit": ["esc", "q"],
"listUp": ["up", "k"],
"listDown": ["down", "j"],
"pinnedDirectory": ["ctrl+p", ""],
"closeFilePanel": ["ctrl+w", ""],
"createNewFilePanel": ["ctrl+n", ""],
"nextFilePanel": ["tab", ""],
"previousFilePanel": ["shift+left", ""],
"focusOnProcessBar": ["p", ""],
"focusOnSideBar": ["b", ""],
"focusOnMetaData": ["m", ""],
"changePanelMode": ["v", ""],
"filePanelFolderCreate": ["f", ""],
"filePanelFileCreate": ["c", ""],
"filePanelItemRename": ["r", ""],
"pasteItem": ["ctrl+v", ""],
"extractFile": ["ctrl+t"],
"compressFile": ["ctrl+y"],
"toggleDotFile": ["ctrl+h", ""],
"_COMMIT_special_hotkey": "These hotkeys do not conflict with any other keys (including global hotkey)",
"cancel": ["ctrl+c", "esc"],
"confirm": ["enter", ""],
"_COMMIT_normal_mode_hotkey": "Here is normal mode hotkey you can conflicts with other mode (cant conflicts with global hotkey)",
"deleteItem": ["ctrl+d", ""],
"selectItem": ["enter", "l"],
"parentDirectory": ["h", "backspace"],
"copySingleItem": ["ctrl+c", ""],
"cutSingleItem": ["ctrl+x", ""],
"_COMMIT_select_mode_hotkey": "Here is select mode hotkey you can conflicts with other mode (cant conflicts with global hotkey)",
"filePanelSelectModeItemSingleSelect": ["enter", "l"],
"filePanelSelectModeItemSelectDown": ["shift+down", "J"],
"filePanelSelectModeItemSelectUp": ["shift+up", "K"],
"filePanelSelectModeItemDelete": ["ctrl+d", "delete"],
"filePanelSelectModeItemCopy": ["ctrl+c", ""],
"filePanelSelectModeItemCut": ["ctrl+x", ""],
"filePanelSelectAllItem": ["ctrl+a", ""],
"_COMMIT_process_bar_hotkey": "Here is process bar panel hotkey you can conflicts with other mode (cant conflicts global hotkey)"
}`
5 changes: 4 additions & 1 deletion src/superfile/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
"filePanelFileCreate": ["c", ""],
"filePanelItemRename": ["r", ""],
"pasteItem": ["ctrl+v", ""],
"extractFile": ["ctrl+t"],
"compressFile": ["ctrl+y"],

"toggleDotFile": ["ctrl+h", ""],

"_COMMIT_special_hotkey": "These hotkeys do not conflict with any other keys (including global hotkey)",
Expand All @@ -41,7 +44,7 @@
"_COMMIT_normal_mode_hotkey": "Here is normal mode hotkey you can conflicts with other mode (cant conflicts with global hotkey)",
"deleteItem": ["ctrl+d", ""],
"selectItem": ["enter", "l"],
"parentFolder": ["h", "backspace"],
"parentDirectory": ["h", "backspace"],
"copySingleItem": ["ctrl+c", ""],
"cutSingleItem": ["ctrl+x", ""],

Expand Down

0 comments on commit bc8cbd7

Please sign in to comment.