feat: add tenant entry registration APIs
This commit is contained in:
@@ -262,6 +262,29 @@ func (oh *ossHandler) Upload(fdef *types.FileDef, file io.Reader) (string, int64
|
||||
return oh.conf.ServeURL + fname, rc.count, nil
|
||||
}
|
||||
|
||||
func (oh *ossHandler) UploadPublic(fdef *media.PublicFileDef, file io.Reader) (string, int64, error) {
|
||||
key, err := publicObjectKey(fdef.Purpose, fdef.ID, fdef.CreatedAt)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
rc := readerCounter{reader: file}
|
||||
options := []aliyunoss.Option{aliyunoss.CacheControl(oh.conf.CacheControl)}
|
||||
if fdef.MimeType != "" {
|
||||
options = append(options, aliyunoss.ContentType(fdef.MimeType))
|
||||
}
|
||||
if err = oh.bucket.PutObject(key, &rc, options...); err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
fdef.Location = key
|
||||
fdef.ETag = etagFromLocation(key)
|
||||
if meta, err := oh.bucket.GetObjectDetailedMeta(key); err == nil {
|
||||
if etag := strings.Trim(meta.Get("ETag"), "\""); etag != "" {
|
||||
fdef.ETag = etag
|
||||
}
|
||||
}
|
||||
return key, rc.count, nil
|
||||
}
|
||||
|
||||
// Download is unsupported because OSS objects are served through signed redirects.
|
||||
func (oh *ossHandler) Download(tenantID types.TenantID, url string) (*types.FileDef, media.ReadSeekCloser, error) {
|
||||
return nil, nil, types.ErrUnsupported
|
||||
@@ -308,6 +331,27 @@ func tenantObjectKey(tenantID types.TenantID, fileID string, createdAt time.Time
|
||||
createdAt.Format("2006"), createdAt.Format("01"), fileID), nil
|
||||
}
|
||||
|
||||
func publicObjectKey(purpose, fileID string, createdAt time.Time) (string, error) {
|
||||
purpose = cleanPublicPurpose(purpose)
|
||||
if purpose == "" || fileID == "" {
|
||||
return "", types.ErrMalformed
|
||||
}
|
||||
if createdAt.IsZero() {
|
||||
createdAt = time.Now().UTC()
|
||||
}
|
||||
return path.Join("public", purpose, createdAt.Format("2006"), createdAt.Format("01"), strings.Trim(fileID, "/")), nil
|
||||
}
|
||||
|
||||
func cleanPublicPurpose(purpose string) string {
|
||||
purpose = strings.Trim(strings.ToLower(strings.TrimSpace(purpose)), "/")
|
||||
return strings.Map(func(r rune) rune {
|
||||
if r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '_' || r == '-' {
|
||||
return r
|
||||
}
|
||||
return -1
|
||||
}, purpose)
|
||||
}
|
||||
|
||||
func etagFromLocation(location string) string {
|
||||
hasher := fnv.New128()
|
||||
hasher.Write([]byte(location))
|
||||
|
||||
Reference in New Issue
Block a user