MCPcopy Create free account
hub / github.com/MertJSX/folderhost / ReadFile

Function ReadFile

routes/read_file.go:13–71  ·  view source on GitHub ↗
(c *fiber.Ctx)

Source from the content-addressed store, hash-verified

11)
12
13func ReadFile(c *fiber.Ctx) error {
14 var path string
15 var fileName string
16 var lastModified string
17 var itemStat os.FileInfo
18 var err error
19 config := &config.Config
20
21 if !c.Locals("account").(types.Account).Permissions.ReadFiles {
22 return c.Status(403).JSON(fiber.Map{"err": "No permission!"})
23 }
24
25 if c.Query("filepath") == "" {
26 return c.Status(400).JSON(fiber.Map{"err": "Bad request!"})
27 }
28
29 path = c.Query("filepath")
30 scope := c.Locals("account").(types.Account).Scope
31
32 if utils.IsNotExistingPath(fmt.Sprintf("%s%s", config.GetScopedFolder(scope), path)) {
33 return c.Status(400).JSON(fiber.Map{"err": "Filepath is not existing!"})
34 }
35
36 itemStat, err = os.Stat(fmt.Sprintf("%s%s", config.GetScopedFolder(scope), path))
37
38 fileName = itemStat.Name()
39 lastModified = itemStat.ModTime().GoString()
40
41 if err != nil {
42 return c.Status(500).JSON(fiber.Map{"err": "Unknown server error!"})
43 }
44
45 if itemStat.IsDir() {
46 return c.Status(400).JSON(fiber.Map{"err": "Filepath is directory!"})
47 }
48
49 if itemStat.Size() > 200*1024 {
50 return c.Status(413).JSON(fiber.Map{"err": "File is too large!"})
51 }
52
53 if remainingSize, _ := utils.GetRemainingFolderSpace(); remainingSize < 200*1024 {
54 return c.Status(413).JSON(fiber.Map{"err": "Not enough storage space to edit! Try to close unused CodeEditor windows. Each code editor window guarantees itself 200 KB of space."})
55 }
56
57 content, err := os.ReadFile(fmt.Sprintf("%s%s", config.GetScopedFolder(scope), path))
58
59 if err != nil {
60 fmt.Printf("Error while reading file: %v\n", err)
61 return c.Status(500).JSON(fiber.Map{"err": "Error while reading file!"})
62 }
63
64 return c.Status(200).JSON(fiber.Map{
65 "data": string(content),
66 "res": "Successfully readed!",
67 "title": fileName,
68 "lastModified": lastModified,
69 "writePermission": c.Locals("account").(types.Account).Permissions.Change,
70 })

Callers

nothing calls this directly

Calls 3

IsNotExistingPathFunction · 0.92
GetRemainingFolderSpaceFunction · 0.92
GetScopedFolderMethod · 0.80

Tested by

no test coverage detected