-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create websocket connection to watch files
- Loading branch information
1 parent
200f1fd
commit 80c0117
Showing
10 changed files
with
263 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
PORT=3000 | ||
PORT=3000 | ||
TRANSFER_FOLDER=./files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,8 @@ DropMyFile | |
# Go workspace file | ||
go.work | ||
|
||
files | ||
# File folder | ||
files | ||
|
||
# system | ||
.DS_store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package folderwatch | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/buildtheui/DropMyFile/pkg/models" | ||
"github.com/buildtheui/DropMyFile/pkg/network" | ||
"github.com/fsnotify/fsnotify" | ||
) | ||
|
||
func WatchFileChanges(folderChange chan<- []string) { | ||
// Specify the folder to watch | ||
folderPath := os.Getenv("TRANSFER_FOLDER") | ||
|
||
// Create new watcher. | ||
watcher, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer watcher.Close() | ||
|
||
// Start listening for events. | ||
go func() { | ||
for { | ||
select { | ||
case event, ok := <-watcher.Events: | ||
if !ok { | ||
return | ||
} | ||
// Notify folder changed | ||
folderChange <- []string{event.Name} | ||
case err, ok := <-watcher.Errors: | ||
if !ok { | ||
return | ||
} | ||
log.Println("error:", err) | ||
} | ||
} | ||
}() | ||
|
||
// Watches the transfer folder for changes | ||
err = watcher.Add(folderPath) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Wait forever to keep watching | ||
select {} | ||
} | ||
|
||
func GetTransferFilesInfo() ([]models.FileInfo, error) { | ||
// Specify the folder to watch | ||
folderPath := os.Getenv("TRANSFER_FOLDER") | ||
|
||
files, err := os.ReadDir(folderPath) | ||
if err != nil { | ||
fmt.Println("Error reading directory:", err) | ||
return nil, err | ||
} | ||
|
||
// Create a slice to store FileInfo structs | ||
var fileInfos []models.FileInfo | ||
|
||
// Iterate over files and populate FileInfo structs | ||
for _, file := range files { | ||
if !file.IsDir() { | ||
downloadLink := network.GetServerAddr(fmt.Sprintf("/api/v1/download/%s", file.Name())) | ||
|
||
// Create a FileInfo struct for the current file | ||
fileInfo := models.FileInfo{ | ||
FileName: file.Name(), | ||
DownloadLink: downloadLink, | ||
} | ||
|
||
// Append FileInfo to the slice | ||
fileInfos = append(fileInfos, fileInfo) | ||
} | ||
} | ||
|
||
return fileInfos, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package models | ||
|
||
// FileInfo represents information about a file, including its name and download link. | ||
type FileInfo struct { | ||
FileName string `json:"fileName"` | ||
DownloadLink string `json:"downloadLink"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.