-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(blob): provide the upload object url endpoint (#120)
Because artifact will start to provide the object upload url This commit implements the handler
- Loading branch information
Showing
7 changed files
with
172 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.uber.org/zap" | ||
|
||
"github.com/gofrs/uuid" | ||
"github.com/instill-ai/artifact-backend/pkg/customerror" | ||
"github.com/instill-ai/artifact-backend/pkg/logger" | ||
artifactpb "github.com/instill-ai/protogen-go/artifact/artifact/v1alpha" | ||
) | ||
|
||
// GetObjectUploadURL returns the upload URL for an object. | ||
func (ph *PublicHandler) GetObjectUploadURL(ctx context.Context, req *artifactpb.GetObjectUploadURLRequest) (*artifactpb.GetObjectUploadURLResponse, error) { | ||
log, _ := logger.GetZapLogger(ctx) | ||
authUID, err := getUserUIDFromContext(ctx) | ||
if err != nil { | ||
err := fmt.Errorf("failed to get user id from header: %v. err: %w", err, customerror.ErrUnauthenticated) | ||
return nil, err | ||
} | ||
creatorUID, err := uuid.FromString(authUID) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse creator uid. err: %w", err) | ||
} | ||
|
||
ns, err := ph.service.GetNamespaceByNsID(ctx, req.GetNamespaceId()) | ||
if err != nil { | ||
log.Error( | ||
"failed to get namespace ", | ||
zap.Error(err), | ||
zap.String("owner_id(ns_id)", req.GetNamespaceId()), | ||
zap.String("auth_uid", authUID)) | ||
return nil, fmt.Errorf("failed to get namespace. err: %w", err) | ||
} | ||
// ACL - check user's permission to upload object in the namespace | ||
err = ph.service.CheckNamespacePermission(ctx, ns) | ||
if err != nil { | ||
log.Error( | ||
"failed to check namespace permission", | ||
zap.Error(err), | ||
zap.String("owner_id(ns_id)", req.GetNamespaceId()), | ||
zap.String("auth_uid", authUID)) | ||
return nil, fmt.Errorf("failed to check namespace permission. err: %w", err) | ||
} | ||
|
||
// Call the service to get the upload URL | ||
response, err := ph.service.GetUploadURL(ctx, req, ns.NsUID, creatorUID) | ||
if err != nil { | ||
log.Error("failed to get upload URL", zap.Error(err)) | ||
return nil, fmt.Errorf("failed to get upload URL. err: %w", err) | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
func (ph *PublicHandler) GetObjectDownloadURL(ctx context.Context, req *artifactpb.GetObjectDownloadURLRequest) (*artifactpb.GetObjectDownloadURLResponse, error) { | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package utils | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// DetermineMimeType determines the MIME type based on the file extension | ||
func DetermineMimeType(fileName string) string { | ||
ext := strings.ToLower(filepath.Ext(fileName)) | ||
switch ext { | ||
case ".pdf": | ||
return "application/pdf" | ||
case ".md": | ||
return "text/markdown" | ||
case ".txt": | ||
return "text/plain" | ||
case ".doc": | ||
return "application/msword" | ||
case ".docx": | ||
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document" | ||
case ".html": | ||
return "text/html" | ||
case ".ppt": | ||
return "application/vnd.ms-powerpoint" | ||
case ".pptx": | ||
return "application/vnd.openxmlformats-officedocument.presentationml.presentation" | ||
case ".xlsx": | ||
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | ||
case ".xls": | ||
return "application/vnd.ms-excel" | ||
case ".csv": | ||
return "text/csv" | ||
case ".json": | ||
return "application/json" | ||
case ".xml": | ||
return "application/xml" | ||
case ".zip": | ||
return "application/zip" | ||
case ".tar": | ||
return "application/x-tar" | ||
case ".gz": | ||
return "application/gzip" | ||
case ".jpg", ".jpeg": | ||
return "image/jpeg" | ||
case ".png": | ||
return "image/png" | ||
case ".gif": | ||
return "image/gif" | ||
case ".svg": | ||
return "image/svg+xml" | ||
case ".mp3": | ||
return "audio/mpeg" | ||
case ".mp4": | ||
return "video/mp4" | ||
case ".wav": | ||
return "audio/wav" | ||
case ".avi": | ||
return "video/x-msvideo" | ||
case ".mpg", ".mpeg": | ||
return "video/mpeg" | ||
case ".ogg": | ||
return "audio/ogg" | ||
case ".css": | ||
return "text/css" | ||
case ".js": | ||
return "application/javascript" | ||
default: | ||
return "application/octet-stream" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters