-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Serve LFS/attachment with http.ServeContent to Support Range-Request #18448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
55241a3
Serve LFS/attachment with http.ServeContent to Support Range-Request
marxangels 34fa2ce
ctx.Resp.Header().Set("Content-Type", st.Mime())
marxangels f2ee607
Update download_test.go
marxangels d085944
Update Cache-Control
marxangels 0f7f583
Add a Comment
marxangels 1953d84
add testes
marxangels 37813a6
Update typesniffer.go
marxangels c6630dd
Update repo.go
marxangels 6743d56
Update download_test.go
marxangels 8deb747
Merge branch 'main' into patch-1
6543 a456cde
Merge branch 'master' into patch-1
6543 ea62aab
ajust
6543 7c60fc8
rm some unrelated diff
6543 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Binary file added
BIN
+18 Bytes
...gitea-repositories-meta/user2/repo2.git/objects/25/2b2353ba950ba3c0457c1acf214937e168db68
Binary file not shown.
Binary file added
BIN
+142 Bytes
...gitea-repositories-meta/user2/repo2.git/objects/80/8eedf6b8dd519aa89b59af2d815ed668580fc2
Binary file not shown.
Binary file added
BIN
+148 Bytes
...gitea-repositories-meta/user2/repo2.git/objects/ee/c3fee8b8e28307e5c8c9099fac5eb583f797b4
Binary file not shown.
2 changes: 1 addition & 1 deletion
2
integrations/gitea-repositories-meta/user2/repo2.git/refs/heads/master
This file contains hidden or 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 |
---|---|---|
@@ -1 +1 @@ | ||
1032bbf17fbc0d9c95bb5418dabe8f8c99278700 | ||
808eedf6b8dd519aa89b59af2d815ed668580fc2 |
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -4,7 +4,12 @@ | |
|
||
package setting | ||
|
||
import "strings" | ||
import ( | ||
"mime" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
) | ||
|
||
// MimeTypeMap defines custom mime type mapping settings | ||
var MimeTypeMap = struct { | ||
|
@@ -21,6 +26,10 @@ func newMimeTypeMap() { | |
m := make(map[string]string, len(keys)) | ||
for _, key := range keys { | ||
m[strings.ToLower(key.Name())] = key.Value() | ||
err := mime.AddExtensionType(key.Name(), key.Value()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please dont add unrelated changes in here |
||
if err != nil { | ||
log.Warn("mime.AddExtensionType(%s,%s): %v", key.Name(), key.Value(), err) | ||
} | ||
} | ||
MimeTypeMap.Map = m | ||
if len(keys) > 0 { | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -7,7 +7,9 @@ package typesniffer | |
import ( | ||
"fmt" | ||
"io" | ||
"mime" | ||
"net/http" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
|
||
|
@@ -36,32 +38,32 @@ type SniffedType struct { | |
|
||
// IsText etects if content format is plain text. | ||
func (ct SniffedType) IsText() bool { | ||
return strings.Contains(ct.contentType, "text/") | ||
return strings.HasPrefix(ct.contentType, "text/") | ||
} | ||
|
||
// IsImage detects if data is an image format | ||
func (ct SniffedType) IsImage() bool { | ||
return strings.Contains(ct.contentType, "image/") | ||
return strings.HasPrefix(ct.contentType, "image/") | ||
} | ||
|
||
// IsSvgImage detects if data is an SVG image format | ||
func (ct SniffedType) IsSvgImage() bool { | ||
return strings.Contains(ct.contentType, SvgMimeType) | ||
return strings.HasPrefix(ct.contentType, SvgMimeType) | ||
} | ||
|
||
// IsPDF detects if data is a PDF format | ||
func (ct SniffedType) IsPDF() bool { | ||
return strings.Contains(ct.contentType, "application/pdf") | ||
return strings.HasPrefix(ct.contentType, "application/pdf") | ||
} | ||
|
||
// IsVideo detects if data is an video format | ||
func (ct SniffedType) IsVideo() bool { | ||
return strings.Contains(ct.contentType, "video/") | ||
return strings.HasPrefix(ct.contentType, "video/") | ||
} | ||
|
||
// IsAudio detects if data is an video format | ||
func (ct SniffedType) IsAudio() bool { | ||
return strings.Contains(ct.contentType, "audio/") | ||
return strings.HasPrefix(ct.contentType, "audio/") | ||
} | ||
|
||
// IsRepresentableAsText returns true if file content can be represented as | ||
|
@@ -70,6 +72,11 @@ func (ct SniffedType) IsRepresentableAsText() bool { | |
return ct.IsText() || ct.IsSvgImage() | ||
} | ||
|
||
// Mime return the mime | ||
func (ct SniffedType) Mime() string { | ||
return strings.Split(ct.contentType, ";")[0] | ||
} | ||
|
||
// DetectContentType extends http.DetectContentType with more content types. Defaults to text/unknown if input is empty. | ||
func DetectContentType(data []byte) SniffedType { | ||
if len(data) == 0 { | ||
|
@@ -82,15 +89,35 @@ func DetectContentType(data []byte) SniffedType { | |
data = data[:sniffLen] | ||
} | ||
|
||
if (strings.Contains(ct, "text/plain") || strings.Contains(ct, "text/html")) && svgTagRegex.Match(data) || | ||
strings.Contains(ct, "text/xml") && svgTagInXMLRegex.Match(data) { | ||
if (strings.HasPrefix(ct, "text/plain") || strings.HasPrefix(ct, "text/html")) && svgTagRegex.Match(data) || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not keep |
||
strings.HasPrefix(ct, "text/xml") && svgTagInXMLRegex.Match(data) { | ||
// SVG is unsupported. https://github.com/golang/go/issues/15888 | ||
ct = SvgMimeType | ||
} | ||
|
||
return SniffedType{ct} | ||
} | ||
|
||
// DetectContentTypeExtFirst | ||
// detect content type by `name` first, if not found, detect by `reader` | ||
// Note: you may need `reader.Seek(0, io.SeekStart)` to reset the offset | ||
func DetectContentTypeExtFirst(name string, bytesOrReader interface{}) (SniffedType, error) { | ||
ct := mime.TypeByExtension(filepath.Ext(name)) | ||
// FIXME: Not sure if it's necessary to keep the old behavior. | ||
// if ct != "" && !strings.HasPrefix(ct, "text/") { | ||
if ct != "" { | ||
return SniffedType{ct}, nil | ||
} | ||
if r, ok := bytesOrReader.(io.Reader); ok { | ||
st, err := DetectContentTypeFromReader(r) | ||
if nil != err { | ||
return SniffedType{}, err | ||
} | ||
return st, nil | ||
} | ||
return DetectContentType(bytesOrReader.([]byte)), nil | ||
} | ||
|
||
// DetectContentTypeFromReader guesses the content type contained in the reader. | ||
func DetectContentTypeFromReader(r io.Reader) (SniffedType, error) { | ||
buf := make([]byte, sniffLen) | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.