Add SVG studio image support, and studio image caching (#418)

* Support SVGs for studio images and add ETAGs
* Add SVG to studio image input
* Update content sniffing
This commit is contained in:
InfiniteTF
2020-04-03 00:11:48 +02:00
committed by GitHub
parent aee9df966b
commit 0f622c84f4
4 changed files with 25 additions and 2 deletions

View File

@@ -2,10 +2,13 @@ package api
import ( import (
"context" "context"
"crypto/md5"
"fmt"
"github.com/go-chi/chi" "github.com/go-chi/chi"
"github.com/stashapp/stash/pkg/models" "github.com/stashapp/stash/pkg/models"
"net/http" "net/http"
"strconv" "strconv"
"strings"
) )
type studioRoutes struct{} type studioRoutes struct{}
@@ -23,6 +26,21 @@ func (rs studioRoutes) Routes() chi.Router {
func (rs studioRoutes) Image(w http.ResponseWriter, r *http.Request) { func (rs studioRoutes) Image(w http.ResponseWriter, r *http.Request) {
studio := r.Context().Value(studioKey).(*models.Studio) studio := r.Context().Value(studioKey).(*models.Studio)
etag := fmt.Sprintf("%x", md5.Sum(studio.Image))
if match := r.Header.Get("If-None-Match"); match != "" {
if strings.Contains(match, etag) {
w.WriteHeader(http.StatusNotModified)
return
}
}
contentType := http.DetectContentType(studio.Image)
if contentType == "text/xml; charset=utf-8" || contentType == "text/plain; charset=utf-8" {
contentType = "image/svg+xml"
}
w.Header().Set("Content-Type", contentType)
w.Header().Add("Etag", etag)
_, _ = w.Write(studio.Image) _, _ = w.Write(studio.Image)
} }

View File

@@ -12,6 +12,7 @@ interface IProps {
onAutoTag?: () => void; onAutoTag?: () => void;
onImageChange: (event: React.FormEvent<HTMLInputElement>) => void; onImageChange: (event: React.FormEvent<HTMLInputElement>) => void;
onBackImageChange?: (event: React.FormEvent<HTMLInputElement>) => void; onBackImageChange?: (event: React.FormEvent<HTMLInputElement>) => void;
acceptSVG?: boolean;
} }
export const DetailsEditNavbar: React.FC<IProps> = (props: IProps) => { export const DetailsEditNavbar: React.FC<IProps> = (props: IProps) => {
@@ -113,6 +114,7 @@ export const DetailsEditNavbar: React.FC<IProps> = (props: IProps) => {
isEditing={props.isEditing} isEditing={props.isEditing}
text={props.onBackImageChange ? "Front image..." : undefined} text={props.onBackImageChange ? "Front image..." : undefined}
onImageChange={props.onImageChange} onImageChange={props.onImageChange}
acceptSVG={props.acceptSVG ?? false}
/> />
{renderBackImageInput()} {renderBackImageInput()}
{renderAutoTagButton()} {renderAutoTagButton()}

View File

@@ -5,12 +5,14 @@ interface IImageInput {
isEditing: boolean; isEditing: boolean;
text?: string; text?: string;
onImageChange: (event: React.FormEvent<HTMLInputElement>) => void; onImageChange: (event: React.FormEvent<HTMLInputElement>) => void;
acceptSVG?: boolean;
} }
export const ImageInput: React.FC<IImageInput> = ({ export const ImageInput: React.FC<IImageInput> = ({
isEditing, isEditing,
text, text,
onImageChange onImageChange,
acceptSVG = false
}) => { }) => {
if (!isEditing) return <div />; if (!isEditing) return <div />;
@@ -20,7 +22,7 @@ export const ImageInput: React.FC<IImageInput> = ({
<Form.Control <Form.Control
type="file" type="file"
onChange={onImageChange} onChange={onImageChange}
accept=".jpg,.jpeg,.png" accept={`.jpg,.jpeg,.png${acceptSVG ? ',.svg' : ''}`}
/> />
</Form.Label> </Form.Label>
); );

View File

@@ -185,6 +185,7 @@ export const Studio: React.FC = () => {
onImageChange={onImageChangeHandler} onImageChange={onImageChangeHandler}
onAutoTag={onAutoTag} onAutoTag={onAutoTag}
onDelete={onDelete} onDelete={onDelete}
acceptSVG
/> />
</div> </div>
{!isNew && ( {!isNew && (