blossom.UploadFilePath() and UploadFile()
This commit is contained in:
@@ -13,18 +13,28 @@ import (
|
||||
"fiatjaf.com/nostr"
|
||||
)
|
||||
|
||||
// UploadFile uploads a file to the media server
|
||||
func (c *Client) UploadFile(ctx context.Context, filePath string) (*BlobDescriptor, error) {
|
||||
// UploadFilePath uploads a file to the media server, takes a filepath
|
||||
func (c *Client) UploadFilePath(ctx context.Context, filePath string) (*BlobDescriptor, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open %s: %w", filePath, err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
bd, err := c.UploadFile(ctx, file, mime.TypeByExtension(filepath.Ext(filePath)))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w -- at path %s", err, filePath)
|
||||
}
|
||||
|
||||
return bd, nil
|
||||
}
|
||||
|
||||
// Upload uploads a file to the media server
|
||||
func (c *Client) UploadFile(ctx context.Context, file *os.File, contentType string) (*BlobDescriptor, error) {
|
||||
sha := sha256.New()
|
||||
size, err := io.Copy(sha, file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read %s: %w", filePath, err)
|
||||
return nil, fmt.Errorf("failed to read file: %w", err)
|
||||
}
|
||||
hash := sha.Sum(nil)
|
||||
|
||||
@@ -33,8 +43,6 @@ func (c *Client) UploadFile(ctx context.Context, filePath string) (*BlobDescript
|
||||
return nil, fmt.Errorf("failed to reset file position: %w", err)
|
||||
}
|
||||
|
||||
contentType := mime.TypeByExtension(filepath.Ext(filePath))
|
||||
|
||||
bd := BlobDescriptor{}
|
||||
err = c.httpCall(ctx, "PUT", "upload", contentType, func() string {
|
||||
return c.authorizationHeader(ctx, func(evt *nostr.Event) {
|
||||
@@ -43,7 +51,7 @@ func (c *Client) UploadFile(ctx context.Context, filePath string) (*BlobDescript
|
||||
})
|
||||
}, file, size, &bd)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to upload %s: %w", filePath, err)
|
||||
return nil, fmt.Errorf("failed to upload: %w", err)
|
||||
}
|
||||
|
||||
return &bd, nil
|
||||
|
||||
Reference in New Issue
Block a user