From 34d995262a87df1a683cc3389e8b353e4fb3beef Mon Sep 17 00:00:00 2001 From: AhmadAwab98 Date: Thu, 30 Nov 2023 15:46:27 +0500 Subject: [PATCH] Changed the Structure - Changed the response structure - Changed the handler accordingly --- handler/handler.go | 53 +++++++++++++++++++++++++++++++++------------- models/model.go | 17 +++++++++++++-- 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/handler/handler.go b/handler/handler.go index 88778f5..09e98ee 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -9,6 +9,7 @@ import ( "strings" "time" "log" + "strconv" ) func ListHandler(w http.ResponseWriter, r *http.Request) { @@ -22,24 +23,35 @@ func ListHandler(w http.ResponseWriter, r *http.Request) { return } responseCache := GetMD5Hash(request.Path) + var finalResponse models.BodyResponse + // var cache models.BodyResponse - var response, cache models.BodyResponse + var response models.InterimBodyResponse - if exists, _ := rdb.Exists(ctx, responseCache).Result(); exists == 1 { - // get the response from cache and convert it into json format - responsecached, _ := rdb.HGetAll(ctx,responseCache).Result() - _ = json.Unmarshal([]byte(responsecached["cachedData"]), &cache) - JSONresponse1, _ := json.MarshalIndent(cache, "", " ") - // respond to client using cache - w.Header().Set("Content-Type", "application/json") - w.Write([]byte(JSONresponse1)) - } + // if exists, _ := rdb.Exists(ctx, responseCache).Result(); exists == 1 { + // // get the response from cache and convert it into json format + // responsecached, _ := rdb.HGetAll(ctx,responseCache).Result() + // _ = json.Unmarshal([]byte(responsecached["cachedData"]), &cache) + // JSONresponse1, _ := json.MarshalIndent(cache, "", " ") + // // respond to client using cache + // w.Header().Set("Content-Type", "application/json") + // w.Write([]byte(JSONresponse1)) + // return + // } // prepare response - recPrepareResponse(request.Path, &response) + recPrepareResponse(request.Path, &response, &finalResponse) + + file, _ := os.Stat(request.Path) + + finalResponse.Status = "success" + finalResponse.Path = request.Path + finalResponse.Contents = append(finalResponse.Contents, response.Contents[0]) + finalResponse.Size = int(file.Size()) / 1024 + // converting to JSON - JSONresponse, errjson := json.Marshal(response) + JSONresponse, errjson := json.Marshal(finalResponse) if errjson != nil { log.Println(err) return @@ -56,9 +68,10 @@ func ListHandler(w http.ResponseWriter, r *http.Request) { // respond to client using actual data w.Header().Set("Content-Type", "application/json") + w.Write([]byte(JSONresponse)) } -func recPrepareResponse(Path string, response *models.BodyResponse) { +func recPrepareResponse(Path string, response *models.InterimBodyResponse, finalResponse *models.BodyResponse) { info, err := os.Stat(Path) // getting the name of the directory/file @@ -70,7 +83,12 @@ func recPrepareResponse(Path string, response *models.BodyResponse) { return case info.IsDir(): // add the folder to content + finalResponse.TFolders++ + response.Type = "folder" response.Name = name[len(name) - 1] + response.Path = Path + response.Size = strconv.Itoa(int(info.Size())) + response.LastModified = info.ModTime().Format("2006-01-02T15:04:05Z") files, err := os.ReadDir(Path) if err != nil { log.Println(err) @@ -78,12 +96,17 @@ func recPrepareResponse(Path string, response *models.BodyResponse) { } for _, file := range files { // call the function recursively on every directory - var subResponse models.BodyResponse - recPrepareResponse(filepath.Join(Path, file.Name()), &subResponse) + var subResponse models.InterimBodyResponse + recPrepareResponse(filepath.Join(Path, file.Name()), &subResponse, finalResponse) response.Contents = append(response.Contents, subResponse) } default: // add files to content + finalResponse.TFiles++ response.Name = name[len(name) - 1] + response.Type = "file" + response.Path = Path + response.Size = strconv.Itoa(int(info.Size())) + response.LastModified = info.ModTime().Format("2006-01-02T15:04:05Z") } } diff --git a/models/model.go b/models/model.go index bf7c1ff..edf6d98 100644 --- a/models/model.go +++ b/models/model.go @@ -6,7 +6,20 @@ type BodyRequest struct { } // represent JSON structure of response body +type InterimBodyResponse struct { + Type string `json:"type" redis:"type"` + Name string `json:"name" redis:"name"` + Path string `json:"path" redis:"path"` + Size string `json:"size" redis:"size"` + LastModified string `json:"lastmodified" redis:"lastmodified"` + Contents []InterimBodyResponse `json:"contents,omitempty" redis:"contents"` +} + type BodyResponse struct { - Name string `json:"Name" redis:"Name"` - Contents []BodyResponse `json:"Contents,omitempty" redis:"Contents"` + Status string `json:"staus" redis:"staus"` + Path string `json:"path" redis:"path"` + Contents []InterimBodyResponse `json:"contents,omitempty" redis:"contents"` + TFiles int `json:"totalFiles" redis:"totalFiles"` + TFolders int `json:"totalFolders" redis:"totalFolders"` + Size int `json:"totalSize" redis:"totalSize"` } \ No newline at end of file