feat: add tenant entry registration APIs
This commit is contained in:
@@ -153,6 +153,29 @@ func (fh *fshandler) Upload(fdef *types.FileDef, file io.Reader) (string, int64,
|
||||
return fh.ServeURL + fname, size, nil
|
||||
}
|
||||
|
||||
func (fh *fshandler) UploadPublic(fdef *media.PublicFileDef, file io.Reader) (string, int64, error) {
|
||||
location, err := publicFileLocation(fh.FileUploadDirectory, fdef.Purpose, fdef.ID, fdef.CreatedAt)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
if err = os.MkdirAll(filepath.Dir(location), 0777); err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
outfile, err := os.Create(location)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
size, err := io.Copy(outfile, file)
|
||||
outfile.Close()
|
||||
if err != nil {
|
||||
os.Remove(location)
|
||||
return "", 0, err
|
||||
}
|
||||
fdef.Location = location
|
||||
fdef.ETag = etagFromPath(location)
|
||||
return location, size, nil
|
||||
}
|
||||
|
||||
// Download processes request for file download.
|
||||
// The returned ReadSeekCloser must be closed after use.
|
||||
func (fh *fshandler) Download(tenantID types.TenantID, url string) (*types.FileDef, media.ReadSeekCloser, error) {
|
||||
@@ -226,6 +249,28 @@ func tenantFileLocation(root string, tenantID types.TenantID, fileID string, cre
|
||||
createdAt.Format("2006"), createdAt.Format("01"), fileID), nil
|
||||
}
|
||||
|
||||
func publicFileLocation(root, purpose, fileID string, createdAt time.Time) (string, error) {
|
||||
purpose = cleanPublicPurpose(purpose)
|
||||
fileID = strings.Trim(fileID, `/\`)
|
||||
if root == "" || purpose == "" || fileID == "" {
|
||||
return "", types.ErrMalformed
|
||||
}
|
||||
if createdAt.IsZero() {
|
||||
createdAt = time.Now().UTC()
|
||||
}
|
||||
return filepath.Join(root, "public", purpose, createdAt.Format("2006"), createdAt.Format("01"), 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 init() {
|
||||
store.RegisterMediaHandler(handlerName, &fshandler{})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user