Skip to content
Snippets Groups Projects
Commit 84f07d55 authored by Ophestra's avatar Ophestra
Browse files

Merge branch 'master' of craft.chars.jp:/srv/git/image-board

parents 1342f0ab 3df7e6f7
No related branches found
Tags v0.7.3
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.
Finish editing this message first!
Please register or to comment