From 77823b1b94ffbfe8bd22fef8eea8757d0adb6026 Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Thu, 28 Oct 2021 23:49:24 +0300 Subject: [PATCH] Started to rewrite the entire thing; The old code is still there for now --- README.md | 8 +- fs/dir.go | 93 ++++++++++++++++++++++++ fs/dir_test.go | 26 +++++++ fs/file.go | 43 +++++++++++ fs/file_test.go | 16 ++++ main.go | 10 +-- node/node.go | 7 ++ node/noder.go | 9 +++ testfiles/testdir/testdir2/testfile3.txt | 9 +++ testfiles/testdir/testfile2.txt | 9 +++ testfiles/testdir2/testfile4 | 3 + testfiles/testfile.txt | 11 +++ 12 files changed, 238 insertions(+), 6 deletions(-) create mode 100644 fs/dir.go create mode 100644 fs/dir_test.go create mode 100644 fs/file.go create mode 100644 fs/file_test.go create mode 100644 node/node.go create mode 100644 node/noder.go create mode 100644 testfiles/testdir/testdir2/testfile3.txt create mode 100644 testfiles/testdir/testfile2.txt create mode 100644 testfiles/testdir2/testfile4 create mode 100644 testfiles/testfile.txt diff --git a/README.md b/README.md index c4b1a99..c0dec36 100644 --- a/README.md +++ b/README.md @@ -103,4 +103,10 @@ Also, this utility only works if the server side has a port-forwarding|virtual s --- ## ● License -MIT \ No newline at end of file +MIT + +## ● TODO +- multiple filepaths as args, not as a flag +- send all files in a directory +- send all files in a directory recursively +- ip address as an arg, not as a flag \ No newline at end of file diff --git a/fs/dir.go b/fs/dir.go new file mode 100644 index 0000000..8a81b1e --- /dev/null +++ b/fs/dir.go @@ -0,0 +1,93 @@ +package fs + +import ( + "fmt" + "os" + "path/filepath" +) + +// A struct that represents the main information about a directory +type Directory struct { + Name string + Path string + ParentPath string + Files []*File + Directories []*Directory +} + +var ErrorNotDirectory error = fmt.Errorf("not a directory") + +func GetDir(path string, recursive bool) (*Directory, error) { + fmt.Println("Provided path ", path) + + absPath, err := filepath.Abs(path) + if err != nil { + return nil, err + } + + fmt.Println("absolute path ", absPath) + + stats, err := os.Stat(absPath) + if err != nil { + return nil, err + } + + // check if it is a directory for real + if !stats.IsDir() { + return nil, ErrorNotDirectory + } + + // loop through each entry in the directory + entries, err := os.ReadDir(absPath) + if err != nil { + return nil, err + } + + var innerDirs []*Directory + var innerFiles []*File + for _, entry := range entries { + entryInfo, err := entry.Info() + if err != nil { + return nil, err + } + + if entryInfo.IsDir() { + if recursive { + // do the recursive magic + innerDirPath := filepath.Join(absPath, entry.Name()) + + fmt.Println("inner dir path ", innerDirPath) + + innerDir, err := GetDir(innerDirPath, true) + if err != nil { + return nil, err + } + + innerDirs = append(innerDirs, innerDir) + } + // if not - skip the directory + + } else { + innerFilePath := filepath.Join(absPath, entryInfo.Name()) + + fmt.Println("inner file path ", innerFilePath) + + innerFile, err := GetFile(innerFilePath) + if err != nil { + return nil, err + } + + innerFiles = append(innerFiles, innerFile) + } + } + + directory := Directory{ + Name: stats.Name(), + Path: absPath, + ParentPath: filepath.Dir(absPath), + Directories: innerDirs, + Files: innerFiles, + } + + return &directory, nil +} diff --git a/fs/dir_test.go b/fs/dir_test.go new file mode 100644 index 0000000..155ea1e --- /dev/null +++ b/fs/dir_test.go @@ -0,0 +1,26 @@ +package fs + +import "testing" + +func Test_GetDir(t *testing.T) { + dirpath := "../testfiles/" + + _, err := GetDir(dirpath, false) + if err != nil { + t.Fatalf("GetDir error: %s", err) + } +} + +func Test_GetDirRecursive(t *testing.T) { + dirpath := "../testfiles/" + + dir, err := GetDir(dirpath, true) + if err != nil { + t.Fatalf("GetDir error: %s", err) + } + + expectedAmountOfUpperDirectories := 2 + if len(dir.Directories) != expectedAmountOfUpperDirectories { + t.Fatalf("GetDir error: expected to have %d inner directories; got %d", expectedAmountOfUpperDirectories, len(dir.Directories)) + } +} diff --git a/fs/file.go b/fs/file.go new file mode 100644 index 0000000..1e7cd79 --- /dev/null +++ b/fs/file.go @@ -0,0 +1,43 @@ +package fs + +import ( + "fmt" + "os" + "path/filepath" +) + +// A struct that represents the main file information +type File struct { + Name string + Path string + ParentPath string + Size uint64 +} + +var ErrorNotFile error = fmt.Errorf("not a file") + +func GetFile(path string) (*File, error) { + absPath, err := filepath.Abs(path) + if err != nil { + return nil, err + } + + stats, err := os.Stat(absPath) + if err != nil { + return nil, err + } + + // check if it is a directory + if stats.IsDir() { + return nil, ErrorNotFile + } + + file := File{ + Name: stats.Name(), + Path: absPath, + ParentPath: filepath.Dir(absPath), + Size: uint64(stats.Size()), + } + + return &file, nil +} diff --git a/fs/file_test.go b/fs/file_test.go new file mode 100644 index 0000000..9dbd7de --- /dev/null +++ b/fs/file_test.go @@ -0,0 +1,16 @@ +package fs + +import "testing" + +func Test_GetFile(t *testing.T) { + filepath := "../testfiles/testfile.txt" + + file, err := GetFile(filepath) + if err != nil { + t.Fatalf("GetFile error: %s", err) + } + + if file.Name != "testfile.txt" { + t.Fatalf("GetFile error: filenames do not match") + } +} diff --git a/main.go b/main.go index 1e7dc63..6a04345 100644 --- a/main.go +++ b/main.go @@ -13,24 +13,24 @@ import ( // flags var ( - PORT *int = flag.Int("port", 8080, "Specifies a port for a sender|port to connect to") + PORT *int = flag.Int("port", 7270, "Specifies a port to work with") SENDERADDR *string = flag.String("addr", "", "Specifies an address to connect to") DOWNLOADSFOLDER *string = flag.String("downloadto", ".", "Specifies where the receiver will store downloaded file") SHAREDFILE *string = flag.String("sharefile", "", "Specifies what file sender will send") - LICENSE *bool = flag.Bool("license", false, "Prints a license text") + PRINTLICENSE *bool = flag.Bool("license", false, "Prints a license text") SENDING bool //go:embed LICENSE - LicenseText string + licenseText string ) // parse flags, validate given values func init() { flag.Parse() - if *LICENSE { - fmt.Println(LicenseText) + if *PRINTLICENSE { + fmt.Println(licenseText) os.Exit(0) } diff --git a/node/node.go b/node/node.go new file mode 100644 index 0000000..3787b91 --- /dev/null +++ b/node/node.go @@ -0,0 +1,7 @@ +package node + + +// Server and a client in one type ! +type Node struct { + +} diff --git a/node/noder.go b/node/noder.go new file mode 100644 index 0000000..557d89f --- /dev/null +++ b/node/noder.go @@ -0,0 +1,9 @@ +package node + +// Implementation for sender and receiver node +type Noder interface { + Connect(addr string, port uint) error + Disconnect() error + Listen(dataPipe chan []byte) + Send(data []byte) error +} diff --git a/testfiles/testdir/testdir2/testfile3.txt b/testfiles/testdir/testdir2/testfile3.txt new file mode 100644 index 0000000..70cab53 --- /dev/null +++ b/testfiles/testdir/testdir2/testfile3.txt @@ -0,0 +1,9 @@ +testfile3 + + +SAMPLE TEXT + +lCtrl + Lshift + u +15dc 2ec 15dc + +not on Windows tho AFAIK diff --git a/testfiles/testdir/testfile2.txt b/testfiles/testdir/testfile2.txt new file mode 100644 index 0000000..2d9bee5 --- /dev/null +++ b/testfiles/testdir/testfile2.txt @@ -0,0 +1,9 @@ +testfile2 + + + +there are no buses +in Gensokyo btw (9) + + +yeah... \ No newline at end of file diff --git a/testfiles/testdir2/testfile4 b/testfiles/testdir2/testfile4 new file mode 100644 index 0000000..676cf1e --- /dev/null +++ b/testfiles/testdir2/testfile4 @@ -0,0 +1,3 @@ +testfile4 + +*something smart and philosophic* \ No newline at end of file diff --git a/testfiles/testfile.txt b/testfiles/testfile.txt new file mode 100644 index 0000000..99a7032 --- /dev/null +++ b/testfiles/testfile.txt @@ -0,0 +1,11 @@ +727 WYSI Airman Badeu square + + + +doable + + +FCeeeeeeeeeeeeeeeeeeeeeeeee + + +testfile it is \ No newline at end of file