Skip to content
Snippets Groups Projects
Commit 3df7e6f7 authored by Ophestra's avatar Ophestra
Browse files

image file get via snowflake, check if file not exist in snowflake lookup

parent 2e6ace1a
No related branches found
No related tags found
No related merge requests found
......@@ -294,6 +294,42 @@ func registerAPI() {
context.JSON(http.StatusOK, instance.ImageSnowflake(flake))
})
router.GET("/api/image/snowflake/:flake/file", func(context *gin.Context) {
flake := context.Param("flake")
if _, err := strconv.Atoi(flake); err != nil {
context.JSON(http.StatusBadRequest, gin.H{
"error": "invalid snowflake",
})
return
}
image, data := instance.ImageData(instance.ImageSnowflakeHash(flake), false)
if image.Hash == "" {
context.JSON(http.StatusNotFound, gin.H{
"error": "not found",
})
return
}
context.Data(http.StatusOK, "image/"+image.Type, data)
})
router.GET("/api/image/snowflake/:flake/preview", func(context *gin.Context) {
flake := context.Param("flake")
if _, err := strconv.Atoi(flake); err != nil {
context.JSON(http.StatusBadRequest, gin.H{
"error": "invalid snowflake",
})
return
}
image, data := instance.ImageData(instance.ImageSnowflakeHash(flake), true)
if image.Hash == "" {
context.JSON(http.StatusNotFound, gin.H{
"error": "not found",
})
return
}
context.Data(http.StatusOK, "image/jpeg", data)
})
router.GET("/api/image", func(context *gin.Context) {
info, ok := getUser(context)
......
......@@ -64,6 +64,9 @@ func (s *Store) Image(hash string) Image {
func (s *Store) ImageMetadataRead(path string) Image {
var metadata Image
if payload, err := os.ReadFile(path); err != nil {
if os.IsNotExist(err) {
return Image{}
}
s.fatalClose(fmt.Sprintf("Error reading image metadata %s, %s", path, err))
} else {
if err = json.Unmarshal(payload, &metadata); err != nil {
......@@ -246,6 +249,9 @@ func (s *Store) ImageSnowflakeHash(flake string) string {
return s.ImageMetadataRead(s.ImageSnowflakePath(flake) + "/" + infoJson).Hash
} else {
if path, err := os.ReadFile(s.ImageSnowflakePath(flake)); err != nil {
if os.IsNotExist(err) {
return ""
}
s.fatalClose(fmt.Sprintf("Error reading snowflake %s association file, %s", flake, err))
} else {
return s.ImageMetadataRead(string(path) + "/" + infoJson).Hash
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment