Bläddra i källkod

added download feature

Toby Chui 6 år sedan
förälder
incheckning
f05ebe728c
3 ändrade filer med 117 tillägg och 5 borttagningar
  1. BIN
      arozdfs.exe
  2. 8 0
      example commands.txt
  3. 109 5
      main.go

BIN
arozdfs.exe


+ 8 - 0
example commands.txt

@@ -0,0 +1,8 @@
+arozdfs open -uuid "7cce7b3c-cdf4-468f-9eac-17917f313900" -outfile "tmp/out.mp4" -c
+tmp/ 7cce7b3c-cdf4-468f-9eac-17917f313900 tmp/out.mp4
+
+arozdfs download -vdir test.mp4
+
+arozdfs upload -uuid "7cce7b3c-cdf4-468f-9eac-17917f313900" -storepath "chunks\7cce7b3c-cdf4-468f-9eac-17917f313900" -vdir "test.mp4"
+
+arozdfs slice -infile uploads/file.mp4 -slice 4

+ 109 - 5
main.go

@@ -14,6 +14,7 @@ import (
 	"path/filepath"
 	"strconv"
 	"strings"
+	"time"
 
 	"github.com/FossoresLP/go-uuid-v4"
 )
@@ -198,6 +199,97 @@ func main() {
 }
 
 func startDownloadProc() {
+	vdir := ""
+	storepath := "tmp/"
+	for i, arg := range os.Args {
+		if strpos(arg, "-") == 0 {
+			//This is a parameter defining keyword
+			if arg == "-vdir" {
+				vdir = os.Args[i+1]
+			} else if arg == "-storepath" {
+				storepath = os.Args[i+1]
+				//Check if the storepath is end with /. if not, append it into the pathname
+				if storepath[len(storepath)-1:] != "/" {
+					storepath = storepath + "/"
+				}
+			}
+		}
+	}
+	if vdir != "" {
+		//Go ahead the download process and get the content of the file
+		fc := strings.Trim(str_replace("\r\n", "\n", file_get_contents("index/"+vdir+".index")), "\n")
+		datachunks := explode("\n", fc)
+		var filelist []string
+		var locations []string
+		//Load config from clusterSetting.config
+		jsonFile, _ := os.Open("clusterSetting.config")
+		byteValue, _ := ioutil.ReadAll(jsonFile)
+		var config clusterConfig
+		json.Unmarshal(byteValue, &config)
+		for i := 0; i < len(datachunks); i++ {
+			tmp := explode(",", datachunks[i])
+			filechunk := tmp[0]
+			locationUUID := tmp[1]
+			filelist = append(filelist, filechunk)
+			thisip := resolveUUID(locationUUID)
+			clusterConfig := ":" + string(config.Port) + "/" + string(config.Prefix) + "/"
+			fullip := "http://" + thisip + clusterConfig
+			locations = append(locations, fullip)
+		}
+		fmt.Println(filelist)
+		fmt.Println(locations)
+
+		//Start the request process
+		for j := 0; j < len(filelist); j++ {
+			//Multithreading download for each fileitem
+			filename := filelist[j]
+			targetURL := locations[j] + "SystemAOB/functions/arozdfs/request.php?chunkuuid=" + string(filename)
+			go downloadFileChunkWithOutput(storepath+filename, targetURL, filename)
+		}
+
+		fileUUID := explode("_", filelist[0])[0] //Getting the first part of a file with uuid, e.g. {uuid}_0 --> get only the {uuid} part
+		//Wait for the go routine to finish
+		downloadFinishIndicators, _ := filepath.Glob(storepath + fileUUID + "_*.done")
+		for len(downloadFinishIndicators) < len(filelist) {
+			time.Sleep(time.Duration(500) * time.Millisecond)
+			downloadFinishIndicators, _ = filepath.Glob(storepath + fileUUID + "_*.done")
+		}
+		//Clear up all indicators
+		for k := 0; k < len(downloadFinishIndicators); k++ {
+			os.Remove(downloadFinishIndicators[k])
+		}
+		fmt.Println("[OK] All chunks downloaded")
+	} else {
+		fmt.Println("ERROR. vdir cannot be empty")
+		os.Exit(0)
+	}
+}
+
+func downloadFileChunkWithOutput(filepath string, url string, filename string) {
+	if DownloadFile(filepath, url) {
+		fmt.Println("[OK] " + filename)
+		file_put_contents(filepath+".done", "")
+	}
+}
+
+func DownloadFile(filepath string, url string) bool {
+
+	// Get the data
+	resp, err := http.Get(url)
+	if err != nil {
+		return false
+	}
+	defer resp.Body.Close()
+
+	// Create the file
+	out, err := os.Create(filepath)
+	if err != nil {
+		return false
+	}
+	defer out.Close()
+	// Write the body to file
+	_, err = io.Copy(out, resp.Body)
+	return true
 
 }
 
@@ -407,6 +499,13 @@ func startUploadProc() {
 		os.Exit(0)
 	}
 	fmt.Println(ipList)
+
+	//Handshake with clusters, create auth token if needed
+	if !createToken(ipList) {
+		fmt.Println("ERROR. Problem occured while trying to create token for one of the cluster's host. Upload process terminated.")
+		os.Exit(0)
+	}
+
 	//Ready to push. Create index file.
 	file_put_contents("index/"+vdir+string(".index"), "")
 	fileList, _ := filepath.Glob(storepath + uuid + "_*")
@@ -439,6 +538,11 @@ func startUploadProc() {
 	fmt.Println("[OK] All chunks uploaded.")
 }
 
+func createToken(ipList []string) bool {
+	//Not implemented
+	return true
+}
+
 func pushFileChunk(uuid string, targetEndpoint string, filename string) string {
 	response := string(SendPostRequest(targetEndpoint+"SystemAOB/functions/arozdfs/upload.php", filename, "file"))
 	return response
@@ -519,10 +623,10 @@ func showHelp() {
 	-storepath <pathname> (Optional) --> Relative path for storing the sliced chunk files, default ./{file-uuid}
 
 	upload
-	-push <remoteDisks.config> --> push to a list of clusters and sync file index to other clusters
 	-storepath <pathname> --> The location where the file chunks are stored
 	-uuid <file uuid> --> uuid of the file to be uploaded
 	-vdir <file.index> --> where the file.index should be stored. (Use for file / folder navigation)
+	-push <remoteDisks.config> (Optional) --> push to a list of clusters and sync file index to other clusters, default ./remoteDisks.config
 
 	[Download from arozdfs commands]
 	download
@@ -530,13 +634,13 @@ func showHelp() {
 	-storepath <tmp directory> (Optional) --> define a special directory for caching the downloaded data chunks, default ./tmp
 
 	open
-	-storepath <tmp directory> --> the file chunks tmp folder, default ./tmp
 	-uuid <file uuid> --> the uuid which the file is stored
 	-outfile <filename> --> filepath for the exported and merged file
-	-c --> remove all stored file chunks after merging the file chunks.
+	-storepath <tmp directory> (Optional)--> the file chunks tmp folder, default ./tmp
+	-c (Optional) --> remove all stored file chunks after merging the file chunks.
 
 	[File Operations]
-	remove <file.index> --> remove all chunks related to thie file index
+	remove <file.index> --> remove all chunks related to this file index
 	rename <file.index> <newfile.index> --> rename all records related to this file
 	move <filepath/file.index> <newpath/file.index> --> move the file to a new path in index directory
 
@@ -548,5 +652,5 @@ func showHelp() {
 }
 
 func showNotFound() {
-	fmt.Println("ERROR. Command not found for function group: " + os.Args[1])
+	fmt.Println("ERROR. Command not found: " + os.Args[1])
 }