mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git), so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form used by the handful of files that had ours. Shape is now: // Copyright 2026 Hanzo AI, Inc. All rights reserved. // Copyright 2015 The Gogs Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT Ours on top, theirs retained verbatim. Retained because MIT requires it, and separately because they are true — those authors did write that code. Not renamed to "The Git Authors": Git is a different project under a different license, and putting its name here would attribute this code to people who have nothing to do with it. Adding our line is the fix; overwriting theirs would have been both a licence violation and a false statement. gofmt still reports import-order drift in some of these files. It predates this change (verified against a stash) and is left alone rather than burying a one-line-per-file legal change under 3000 files of unrelated reordering. Build clean. Co-authored-by: Hanzo Dev <[email protected]>
67 lines
2.0 KiB
Go
67 lines
2.0 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package httplib
|
|
|
|
import (
|
|
"mime"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/git/modules/setting"
|
|
)
|
|
|
|
type ContentDispositionType string
|
|
|
|
const (
|
|
ContentDispositionInline ContentDispositionType = "inline"
|
|
ContentDispositionAttachment ContentDispositionType = "attachment"
|
|
)
|
|
|
|
func needsEncodingRune(b rune) bool {
|
|
return (b < ' ' || b > '~') && b != '\t'
|
|
}
|
|
|
|
// getSafeName replaces all invalid chars in the filename field by underscore
|
|
func getSafeName(s string) (_ string, needsEncoding bool) {
|
|
var out strings.Builder
|
|
for _, b := range s {
|
|
if needsEncodingRune(b) {
|
|
needsEncoding = true
|
|
out.WriteRune('_')
|
|
} else {
|
|
out.WriteRune(b)
|
|
}
|
|
}
|
|
return out.String(), needsEncoding
|
|
}
|
|
|
|
func EncodeContentDispositionAttachment(filename string) string {
|
|
return encodeContentDisposition(ContentDispositionAttachment, filename)
|
|
}
|
|
|
|
func EncodeContentDispositionInline(filename string) string {
|
|
return encodeContentDisposition(ContentDispositionInline, filename)
|
|
}
|
|
|
|
// encodeContentDisposition encodes a correct Content-Disposition Header
|
|
func encodeContentDisposition(t ContentDispositionType, filename string) string {
|
|
safeFilename, needsEncoding := getSafeName(filename)
|
|
result := mime.FormatMediaType(string(t), map[string]string{"filename": safeFilename})
|
|
// No need for the utf8 encoding
|
|
if !needsEncoding {
|
|
return result
|
|
}
|
|
utf8Result := mime.FormatMediaType(string(t), map[string]string{"filename": filename})
|
|
|
|
// The mime package might have unexpected results in other go versions
|
|
// Make tests instance fail, otherwise use the default behavior of the go mime package
|
|
if !strings.HasPrefix(result, string(t)+"; filename=") || !strings.HasPrefix(utf8Result, string(t)+"; filename*=") {
|
|
setting.PanicInDevOrTesting("Unexpected mime package result %s", result)
|
|
return utf8Result
|
|
}
|
|
|
|
encodedFileName := strings.TrimPrefix(utf8Result, string(t))
|
|
return result + encodedFileName
|
|
}
|