🐛 Provide a way to bypass nexus and serving content to prevent nexus reverse proxy issue

This commit is contained in:
2024-11-06 23:01:29 +08:00
parent 2f99ac7b6e
commit c41f8b77ce
9 changed files with 6460 additions and 636 deletions

View File

@ -7,11 +7,8 @@ import (
"gorm.io/datatypes"
)
type AttachmentDst = int8
const (
AttachmentDstTemporary = AttachmentDst(iota)
AttachmentDstPermanent
AttachmentDstTemporary = 0 // The destination 0 is a reserved config for pre-upload processing
)
type Attachment struct {
@ -22,13 +19,13 @@ type Attachment struct {
// Unique ID is for storing (appear in local file name or object name)
Uuid string `json:"uuid"`
Size int64 `json:"size"`
Name string `json:"name"`
Alternative string `json:"alt"`
MimeType string `json:"mimetype"`
HashCode string `json:"hash"`
Destination AttachmentDst `json:"destination"`
RefCount int `json:"ref_count"`
Size int64 `json:"size"`
Name string `json:"name"`
Alternative string `json:"alt"`
MimeType string `json:"mimetype"`
HashCode string `json:"hash"`
Destination int `json:"destination"`
RefCount int `json:"ref_count"`
FileChunks datatypes.JSONMap `json:"file_chunks"`

View File

@ -12,7 +12,8 @@ type BaseDestination struct {
type LocalDestination struct {
BaseDestination
Path string `json:"path"`
Path string `json:"path"`
AccessBaseURL string `json:"access_baseurl"`
}
type S3Destination struct {

View File

@ -26,12 +26,7 @@ func openAttachment(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusNotFound, "file is in uploading progress, please wait until all chunk uploaded")
}
var destMap map[string]any
if metadata.Destination == models.AttachmentDstTemporary {
destMap = viper.GetStringMap("destinations.temporary")
} else {
destMap = viper.GetStringMap("destinations.permanent")
}
destMap := viper.GetStringMap(fmt.Sprintf("destinations.%d", metadata.Destination))
var dest models.BaseDestination
rawDest, _ := jsoniter.Marshal(destMap)
@ -41,10 +36,19 @@ func openAttachment(c *fiber.Ctx) error {
case models.DestinationTypeLocal:
var destConfigured models.LocalDestination
_ = jsoniter.Unmarshal(rawDest, &destConfigured)
if len(destConfigured.AccessBaseURL) > 0 && !c.QueryBool("direct", false) {
// This will drop all query parameters,
// for not it's okay because the openAttachment api won't take any query parameters
return c.Redirect(fmt.Sprintf(
"%s%s?direct=true",
destConfigured.AccessBaseURL,
c.Path(),
), fiber.StatusMovedPermanently)
}
if len(metadata.MimeType) > 0 {
c.Set(fiber.HeaderContentType, metadata.MimeType)
}
return c.SendFile(filepath.Join(destConfigured.Path, metadata.Uuid), false)
return c.SendFile(filepath.Join(destConfigured.Path, metadata.Uuid))
case models.DestinationTypeS3:
var destConfigured models.S3Destination
_ = jsoniter.Unmarshal(rawDest, &destConfigured)

View File

@ -241,7 +241,7 @@ func AnalyzeAttachment(file models.Attachment) error {
// Move temporary to permanent
if !linked {
if err := ReUploadFileToPermanent(file); err != nil {
if err := ReUploadFileToPermanent(file, 1); err != nil {
return fmt.Errorf("unable to move file to permanet storage: %v", err)
}
}

View File

@ -94,12 +94,15 @@ func CheckChunkExistsInTemporary(meta models.Attachment, cid string) bool {
}
}
func ReUploadFileToPermanent(meta models.Attachment) error {
func ReUploadFileToPermanent(meta models.Attachment, dst int) error {
if dst == models.AttachmentDstTemporary || meta.Destination != dst {
return fmt.Errorf("destnation cannot be reversed temporary or the same as the original")
}
if meta.Destination != models.AttachmentDstTemporary {
return fmt.Errorf("attachment isn't in temporary storage, unable to process")
}
meta.Destination = models.AttachmentDstPermanent
meta.Destination = 0
destMap := viper.GetStringMap("destinations.permanent")