Skip to content

Commit

Permalink
Merge pull request #3 from uelei/fix-lock-value
Browse files Browse the repository at this point in the history
Fix lock value and some error check in the code
  • Loading branch information
uelei authored Nov 16, 2022
2 parents f84858c + 877f27e commit 69d9c0a
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -35,38 +34,50 @@ func get_local_path(base_dir string) string {
return path
}

func download_file(c gowebdav.Client, base_dir string, file_name string, server_time time.Time) {
func download_file(c *gowebdav.Client, base_dir string, file_name string, server_time time.Time) {
reader, _ := c.ReadStream(base_dir + file_name)

full_path := filepath.Join(get_local_path(base_dir), file_name)
fmt.Println("Downloading new File ", full_path)
f, _ := os.Create(full_path)
defer f.Close()

io.Copy(f, reader)
_, er := io.Copy(f, reader)

if er != nil {
fmt.Println(er)
}

err := os.Chtimes(full_path, server_time, server_time)
if err != nil {
fmt.Println(err)
}
}

func upload_file(c gowebdav.Client, base_dir string, file_name string) {
func upload_file(c *gowebdav.Client, base_dir string, file_name string) {
full_path := filepath.Join(get_local_path(base_dir), file_name)

fmt.Println("Uploading new File ", full_path)
bytes, _ := ioutil.ReadFile(full_path)
bytes, _ := os.ReadFile(full_path)

c.Write(filepath.Join(base_dir, file_name), bytes, 0644)
er := c.Write(filepath.Join(base_dir, file_name), bytes, 0644)

if er != nil {
fmt.Println(er)
}

fil, _ := os.Open(full_path)
defer fil.Close()

c.WriteStream(filepath.Join(base_dir, file_name), fil, 0644)
}
err := c.WriteStream(filepath.Join(base_dir, file_name), fil, 0644)

if err != nil {
fmt.Println(err)
}

}

func upload_a_local_folder(c gowebdav.Client, base_dir string, local_folder_name string, local_base_path string) {
func upload_a_local_folder(c *gowebdav.Client, base_dir string, local_folder_name string, local_base_path string) {
fmt.Println("uploading local folder ", base_dir, local_folder_name)

err := c.Mkdir(base_dir+local_folder_name, 0644)
Expand All @@ -91,7 +102,7 @@ func upload_a_local_folder(c gowebdav.Client, base_dir string, local_folder_name
}
}

func check_folder(c gowebdav.Client, base_dir string) {
func check_folder(c *gowebdav.Client, base_dir string) {
println("checking base dir = ", base_dir)

sync := make(map[string]time.Time)
Expand All @@ -108,7 +119,7 @@ func check_folder(c gowebdav.Client, base_dir string) {
check_folder(c, base_dir+file.Name()+"/")
} else {
full_path := filepath.Join(path, file.Name())
if doesFileExist(full_path) != true {
if !doesFileExist(full_path) {
download_file(c, base_dir, file.Name(), file.ModTime())
}
sync[base_dir+file.Name()] = file.ModTime()
Expand All @@ -122,8 +133,15 @@ func check_folder(c gowebdav.Client, base_dir string) {
fils, err := os.ReadDir(path)
if err != nil {
fmt.Println("Creating a folder", path)
os.Mkdir(path, os.ModePerm)
for _, file := range files {

er := os.Mkdir(path, os.ModePerm)

if er != nil {
fmt.Println(er)

}

for _, file := range files {
download_file(c, base_dir, file.Name(), file.ModTime())
}
}
Expand Down Expand Up @@ -171,5 +189,5 @@ func main() {
}
c := gowebdav.NewClient(server, user, password)

check_folder(*c, "/notes/")
check_folder(c, "/notes/")
}

0 comments on commit 69d9c0a

Please sign in to comment.