Browse Source

Started to rewrite the entire thing; The old code is still there for now

main
Unbewohnte 3 years ago
parent
commit
77823b1b94
  1. 8
      README.md
  2. 93
      fs/dir.go
  3. 26
      fs/dir_test.go
  4. 43
      fs/file.go
  5. 16
      fs/file_test.go
  6. 10
      main.go
  7. 7
      node/node.go
  8. 9
      node/noder.go
  9. 9
      testfiles/testdir/testdir2/testfile3.txt
  10. 9
      testfiles/testdir/testfile2.txt
  11. 3
      testfiles/testdir2/testfile4
  12. 11
      testfiles/testfile.txt

8
README.md

@ -103,4 +103,10 @@ Also, this utility only works if the server side has a port-forwarding|virtual s
---
## ● License
MIT
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

93
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
}

26
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))
}
}

43
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
}

16
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")
}
}

10
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)
}

7
node/node.go

@ -0,0 +1,7 @@
package node
// Server and a client in one type !
type Node struct {
}

9
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
}

9
testfiles/testdir/testdir2/testfile3.txt

@ -0,0 +1,9 @@
testfile3
SAMPLE TEXT
lCtrl + Lshift + u
15dc 2ec 15dc
not on Windows tho AFAIK

9
testfiles/testdir/testfile2.txt

@ -0,0 +1,9 @@
testfile2
there are no buses
in Gensokyo btw (9)
yeah...

3
testfiles/testdir2/testfile4

@ -0,0 +1,3 @@
testfile4
*something smart and philosophic*

11
testfiles/testfile.txt

@ -0,0 +1,11 @@
727 WYSI Airman Badeu square
doable
FCeeeeeeeeeeeeeeeeeeeeeeeee
testfile it is
Loading…
Cancel
Save