Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"
"log"
"strconv"
)

func ListHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -70,20 +83,30 @@ 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)
return
}
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")
}
}
17 changes: 15 additions & 2 deletions models/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}