-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for player skins via SkinsDB (#284)
* Add support for player skins via SkinsDB * Fix jshint complaints
- Loading branch information
Showing
10 changed files
with
174 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package web | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func (api *Api) GetSkin(resp http.ResponseWriter, req *http.Request) { | ||
filename := strings.TrimPrefix(req.URL.Path, "/api/skins/") | ||
// there should be no remaining path elements - abort if there are any - prevent escaping into FS | ||
if strings.Contains(filename, "/") { | ||
resp.WriteHeader(http.StatusNotFound) | ||
return | ||
} | ||
|
||
// we should only be serving PNG images | ||
if !strings.HasSuffix(filename, ".png") { | ||
resp.WriteHeader(http.StatusNotFound) | ||
return | ||
} | ||
|
||
filePath := api.Context.Config.Skins.SkinsPath + "/" + filename | ||
|
||
content, err := os.ReadFile(filePath) | ||
// make file not found more sensible | ||
if errors.Is(err, os.ErrNotExist) { | ||
resp.WriteHeader(http.StatusNotFound) | ||
return | ||
} else if err != nil { | ||
resp.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// return the file content when available | ||
if content != nil { | ||
resp.Write(content) | ||
resp.Header().Add("content-type", "image/png") | ||
return | ||
} | ||
|
||
// fallback | ||
resp.WriteHeader(http.StatusNotFound) | ||
resp.Write([]byte(filename)) | ||
} |