Working Version 1.0.1
This commit is contained in:
parent
c9d6900d48
commit
899ca31bb3
@ -12,19 +12,19 @@ const (
|
||||
|
||||
EnableNASTransfer = true
|
||||
NASOutputPath = "\\\\HomeLabNAS\\dci\\streams"
|
||||
NASUsername = ""
|
||||
NASPassword = ""
|
||||
NASUsername = "NASAdmin"
|
||||
NASPassword = "s3tkY6tzA&KN6M"
|
||||
TransferWorkerCount = 2
|
||||
TransferRetryLimit = 3
|
||||
TransferTimeout = 30 * time.Second
|
||||
FileSettlingDelay = 5 * time.Second
|
||||
PersistencePath = "../data/transfer_queue.json"
|
||||
TransferQueueSize = 100000
|
||||
BatchSize = 10
|
||||
BatchSize = 1000
|
||||
ManifestPath = "../data"
|
||||
|
||||
CleanupAfterTransfer = true
|
||||
CleanupBatchSize = 10
|
||||
CleanupBatchSize = 1000
|
||||
RetainLocalHours = 0
|
||||
|
||||
AutoProcess = true
|
||||
|
||||
@ -7,7 +7,9 @@ import (
|
||||
"log"
|
||||
"m3u8-downloader/pkg/nas"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type NASTransfer struct {
|
||||
@ -19,6 +21,12 @@ func NewNASTransfer(config nas.NASConfig) *NASTransfer {
|
||||
nt := &NASTransfer{
|
||||
config: config,
|
||||
}
|
||||
|
||||
// Establish network connection with credentials before accessing the path
|
||||
if err := nt.establishConnection(); err != nil {
|
||||
log.Fatalf("Failed to establish network connection to %s: %v", nt.config.Path, err)
|
||||
}
|
||||
|
||||
err := nt.ensureDirectoryExists(nt.config.Path)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create directory %s: %v", nt.config.Path, err)
|
||||
@ -109,6 +117,47 @@ func (nt *NASTransfer) ensureDirectoryExists(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (nt *NASTransfer) establishConnection() error {
|
||||
// Extract the network path (\\server\share) from the full path
|
||||
networkPath := nt.extractNetworkPath(nt.config.Path)
|
||||
if networkPath == "" {
|
||||
// Local path, no authentication needed
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Printf("Establishing network connection to %s with user %s", networkPath, nt.config.Username)
|
||||
|
||||
// Use Windows net use command to establish authenticated connection
|
||||
var cmd *exec.Cmd
|
||||
if nt.config.Username != "" && nt.config.Password != "" {
|
||||
cmd = exec.Command("net", "use", networkPath, nt.config.Password, "/user:"+nt.config.Username, "/persistent:no")
|
||||
} else {
|
||||
cmd = exec.Command("net", "use", networkPath, "/persistent:no")
|
||||
}
|
||||
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to establish network connection: %w\nOutput: %s", err, string(output))
|
||||
}
|
||||
|
||||
log.Printf("Network connection established successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (nt *NASTransfer) extractNetworkPath(fullPath string) string {
|
||||
// Extract \\server\share from paths like \\server\share\folder\subfolder
|
||||
if !strings.HasPrefix(fullPath, "\\\\") {
|
||||
return "" // Not a UNC path
|
||||
}
|
||||
|
||||
parts := strings.Split(fullPath[2:], "\\") // Remove leading \\
|
||||
if len(parts) < 2 {
|
||||
return "" // Invalid UNC path
|
||||
}
|
||||
|
||||
return "\\\\" + parts[0] + "\\" + parts[1]
|
||||
}
|
||||
|
||||
func (nt *NASTransfer) TestConnection() error {
|
||||
testFile := filepath.Join(nt.config.Path, ".connection_test")
|
||||
|
||||
@ -129,6 +178,26 @@ func (nt *NASTransfer) IsConnected() bool {
|
||||
return nt.connected
|
||||
}
|
||||
|
||||
// Disconnect removes the network connection
|
||||
func (nt *NASTransfer) Disconnect() error {
|
||||
networkPath := nt.extractNetworkPath(nt.config.Path)
|
||||
if networkPath == "" {
|
||||
return nil // Local path, nothing to disconnect
|
||||
}
|
||||
|
||||
cmd := exec.Command("net", "use", networkPath, "/delete")
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Printf("Warning: failed to disconnect from %s: %v\nOutput: %s", networkPath, err, string(output))
|
||||
// Don't return error since this is cleanup
|
||||
} else {
|
||||
log.Printf("Disconnected from network path: %s", networkPath)
|
||||
}
|
||||
|
||||
nt.connected = false
|
||||
return nil
|
||||
}
|
||||
|
||||
// FileExists checks if a file already exists on the NAS and optionally verifies size
|
||||
func (nt *NASTransfer) FileExists(destinationPath string, expectedSize int64) (bool, error) {
|
||||
fullDestPath := filepath.Join(nt.config.Path, destinationPath)
|
||||
|
||||
@ -140,6 +140,11 @@ func (ts *TransferService) Shutdown(ctx context.Context) error {
|
||||
return fmt.Errorf("Failed to force cleanup: %w", err)
|
||||
}
|
||||
|
||||
// Disconnect from NAS
|
||||
if err := ts.nas.Disconnect(); err != nil {
|
||||
log.Printf("Warning: failed to disconnect from NAS: %v", err)
|
||||
}
|
||||
|
||||
log.Println("Transfer service shut down")
|
||||
|
||||
return nil
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user