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

Function RawFile

routes/get_rawfile.go:16–79  ·  view source on GitHub ↗
(c *fiber.Ctx)

Source from the content-addressed store, hash-verified

14)
15
16func RawFile(c *fiber.Ctx) error {
17 if !c.Locals("account").(types.Account).Permissions.ReadFiles {
18 return c.Status(403).JSON(fiber.Map{"err": "No permission!"})
19 }
20
21 filepathParam := c.Params("filepath")
22 if filepathParam == "" {
23 return c.Status(400).JSON(fiber.Map{"err": "Filepath is required"})
24 }
25
26 filepathParam, err := url.PathUnescape(filepathParam)
27 if err != nil {
28 return c.Status(400).JSON(fiber.Map{"err": "Invalid filepath encoding"})
29 }
30
31 config := &config.Config
32 scope := c.Locals("account").(types.Account).Scope
33 fullPath := fmt.Sprintf("%s%s", config.GetScopedFolder(scope), filepathParam)
34
35 if utils.IsNotExistingPath(fullPath) {
36 return c.Status(404).JSON(fiber.Map{"err": "File not found"})
37 }
38
39 fileInfo, err := os.Stat(fullPath)
40 if err != nil {
41 return c.Status(500).JSON(fiber.Map{"err": "Could not read file info"})
42 }
43
44 if fileInfo.IsDir() {
45 return c.Status(400).JSON(fiber.Map{"err": "Path is a directory, not a file"})
46 }
47
48 file, err := os.Open(fullPath)
49 if err != nil {
50 return c.Status(500).JSON(fiber.Map{"err": "Could not open file"})
51 }
52 defer file.Close()
53
54 ext := strings.ToLower(filepath.Ext(filepathParam))
55 contentType := "application/octet-stream"
56
57 switch ext {
58 case ".pdf":
59 contentType = "application/pdf"
60 case ".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".svg":
61 contentType = "image/" + strings.TrimPrefix(ext, ".")
62 if ext == ".jpg" {
63 contentType = "image/jpeg"
64 }
65 case ".mp4", ".webm", ".ogg", ".mov", ".avi":
66 contentType = "video/" + strings.TrimPrefix(ext, ".")
67 case ".mp3", ".wav", ".flac", ".m4a", ".opus":
68 contentType = "audio/" + strings.TrimPrefix(ext, ".")
69 default:
70 return c.Status(400).JSON(fiber.Map{"err": "File content type is not supported"})
71 }
72
73 c.Set("Content-Type", contentType)

Callers

nothing calls this directly

Calls 3

IsNotExistingPathFunction · 0.92
GetScopedFolderMethod · 0.80
SetMethod · 0.80

Tested by

no test coverage detected