mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
feat: add visibility field to resource (#1185)
This commit is contained in:
@@ -9,11 +9,12 @@ type Resource struct {
|
|||||||
UpdatedTs int64 `json:"updatedTs"`
|
UpdatedTs int64 `json:"updatedTs"`
|
||||||
|
|
||||||
// Domain specific fields
|
// Domain specific fields
|
||||||
Filename string `json:"filename"`
|
Filename string `json:"filename"`
|
||||||
Blob []byte `json:"-"`
|
Blob []byte `json:"-"`
|
||||||
ExternalLink string `json:"externalLink"`
|
ExternalLink string `json:"externalLink"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Size int64 `json:"size"`
|
Size int64 `json:"size"`
|
||||||
|
Visibility Visibility `json:"visibility"`
|
||||||
|
|
||||||
// Related fields
|
// Related fields
|
||||||
LinkedMemoAmount int `json:"linkedMemoAmount"`
|
LinkedMemoAmount int `json:"linkedMemoAmount"`
|
||||||
@@ -24,11 +25,12 @@ type ResourceCreate struct {
|
|||||||
CreatorID int `json:"-"`
|
CreatorID int `json:"-"`
|
||||||
|
|
||||||
// Domain specific fields
|
// Domain specific fields
|
||||||
Filename string `json:"filename"`
|
Filename string `json:"filename"`
|
||||||
Blob []byte `json:"-"`
|
Blob []byte `json:"-"`
|
||||||
ExternalLink string `json:"externalLink"`
|
ExternalLink string `json:"externalLink"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Size int64 `json:"-"`
|
Size int64 `json:"-"`
|
||||||
|
Visibility Visibility `json:"visibility"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResourceFind struct {
|
type ResourceFind struct {
|
||||||
@@ -50,7 +52,8 @@ type ResourcePatch struct {
|
|||||||
UpdatedTs *int64
|
UpdatedTs *int64
|
||||||
|
|
||||||
// Domain specific fields
|
// Domain specific fields
|
||||||
Filename *string `json:"filename"`
|
Filename *string `json:"filename"`
|
||||||
|
Visibility *Visibility `json:"visibility"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResourceDelete struct {
|
type ResourceDelete struct {
|
||||||
|
@@ -35,7 +35,7 @@ func NewClient(ctx context.Context, config *Config) (*Client, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
})
|
})
|
||||||
|
|
||||||
cfg, err := s3config.LoadDefaultConfig(ctx,
|
awsConfig, err := s3config.LoadDefaultConfig(ctx,
|
||||||
s3config.WithEndpointResolverWithOptions(resolver),
|
s3config.WithEndpointResolverWithOptions(resolver),
|
||||||
s3config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(config.AccessKey, config.SecretKey, "")),
|
s3config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(config.AccessKey, config.SecretKey, "")),
|
||||||
)
|
)
|
||||||
@@ -43,7 +43,7 @@ func NewClient(ctx context.Context, config *Config) (*Client, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
client := awss3.NewFromConfig(cfg)
|
client := awss3.NewFromConfig(awsConfig)
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
Client: client,
|
Client: client,
|
||||||
@@ -53,7 +53,7 @@ func NewClient(ctx context.Context, config *Config) (*Client, error) {
|
|||||||
|
|
||||||
func (client *Client) UploadFile(ctx context.Context, filename string, fileType string, src io.Reader) (string, error) {
|
func (client *Client) UploadFile(ctx context.Context, filename string, fileType string, src io.Reader) (string, error) {
|
||||||
uploader := manager.NewUploader(client.Client)
|
uploader := manager.NewUploader(client.Client)
|
||||||
resp, err := uploader.Upload(ctx, &awss3.PutObjectInput{
|
uploadOutput, err := uploader.Upload(ctx, &awss3.PutObjectInput{
|
||||||
Bucket: aws.String(client.Config.Bucket),
|
Bucket: aws.String(client.Config.Bucket),
|
||||||
Key: aws.String(filename),
|
Key: aws.String(filename),
|
||||||
Body: src,
|
Body: src,
|
||||||
@@ -63,10 +63,12 @@ func (client *Client) UploadFile(ctx context.Context, filename string, fileType
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
var link string
|
|
||||||
if client.Config.URLPrefix == "" {
|
link := uploadOutput.Location
|
||||||
link = resp.Location
|
if link == "" {
|
||||||
} else {
|
if client.Config.URLPrefix == "" {
|
||||||
|
return "", fmt.Errorf("url prefix is empty")
|
||||||
|
}
|
||||||
link = fmt.Sprintf("%s/%s", client.Config.URLPrefix, filename)
|
link = fmt.Sprintf("%s/%s", client.Config.URLPrefix, filename)
|
||||||
}
|
}
|
||||||
return link, nil
|
return link, nil
|
||||||
|
@@ -24,6 +24,10 @@ type Profile struct {
|
|||||||
Version string `json:"version"`
|
Version string `json:"version"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Profile) IsDev() bool {
|
||||||
|
return p.Mode != "prod"
|
||||||
|
}
|
||||||
|
|
||||||
func checkDSN(dataDir string) (string, error) {
|
func checkDSN(dataDir string) (string, error) {
|
||||||
// Convert to absolute path if relative path is supplied.
|
// Convert to absolute path if relative path is supplied.
|
||||||
if !filepath.IsAbs(dataDir) {
|
if !filepath.IsAbs(dataDir) {
|
||||||
|
@@ -42,6 +42,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
|
|||||||
if resourceCreate.ExternalLink != "" && !strings.HasPrefix(resourceCreate.ExternalLink, "http") {
|
if resourceCreate.ExternalLink != "" && !strings.HasPrefix(resourceCreate.ExternalLink, "http") {
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid external link")
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid external link")
|
||||||
}
|
}
|
||||||
|
resourceCreate.Visibility = api.Private
|
||||||
resource, err := s.Store.CreateResource(ctx, resourceCreate)
|
resource, err := s.Store.CreateResource(ctx, resourceCreate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create resource").SetInternal(err)
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create resource").SetInternal(err)
|
||||||
@@ -99,11 +100,12 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
|
|||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to read file").SetInternal(err)
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to read file").SetInternal(err)
|
||||||
}
|
}
|
||||||
resourceCreate = &api.ResourceCreate{
|
resourceCreate = &api.ResourceCreate{
|
||||||
CreatorID: userID,
|
CreatorID: userID,
|
||||||
Filename: filename,
|
Filename: filename,
|
||||||
Type: filetype,
|
Type: filetype,
|
||||||
Size: size,
|
Size: size,
|
||||||
Blob: fileBytes,
|
Blob: fileBytes,
|
||||||
|
Visibility: api.Private,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
storage, err := s.Store.FindStorage(ctx, &api.StorageFind{ID: &storageServiceID})
|
storage, err := s.Store.FindStorage(ctx, &api.StorageFind{ID: &storageServiceID})
|
||||||
@@ -134,6 +136,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
|
|||||||
Filename: filename,
|
Filename: filename,
|
||||||
Type: filetype,
|
Type: filetype,
|
||||||
ExternalLink: link,
|
ExternalLink: link,
|
||||||
|
Visibility: api.Private,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Unsupported storage type")
|
return echo.NewHTTPError(http.StatusInternalServerError, "Unsupported storage type")
|
||||||
|
@@ -76,7 +76,8 @@ CREATE TABLE resource (
|
|||||||
blob BLOB DEFAULT NULL,
|
blob BLOB DEFAULT NULL,
|
||||||
external_link TEXT NOT NULL DEFAULT '',
|
external_link TEXT NOT NULL DEFAULT '',
|
||||||
type TEXT NOT NULL DEFAULT '',
|
type TEXT NOT NULL DEFAULT '',
|
||||||
size INTEGER NOT NULL DEFAULT 0
|
size INTEGER NOT NULL DEFAULT 0,
|
||||||
|
visibility TEXT NOT NULL CHECK (visibility IN ('PUBLIC', 'PROTECTED', 'PRIVATE')) DEFAULT 'PRIVATE'
|
||||||
);
|
);
|
||||||
|
|
||||||
-- memo_resource
|
-- memo_resource
|
||||||
|
@@ -27,6 +27,7 @@ type resourceRaw struct {
|
|||||||
ExternalLink string
|
ExternalLink string
|
||||||
Type string
|
Type string
|
||||||
Size int64
|
Size int64
|
||||||
|
Visibility api.Visibility
|
||||||
}
|
}
|
||||||
|
|
||||||
func (raw *resourceRaw) toResource() *api.Resource {
|
func (raw *resourceRaw) toResource() *api.Resource {
|
||||||
@@ -44,6 +45,7 @@ func (raw *resourceRaw) toResource() *api.Resource {
|
|||||||
ExternalLink: raw.ExternalLink,
|
ExternalLink: raw.ExternalLink,
|
||||||
Type: raw.Type,
|
Type: raw.Type,
|
||||||
Size: raw.Size,
|
Size: raw.Size,
|
||||||
|
Visibility: raw.Visibility,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +90,7 @@ func (s *Store) CreateResource(ctx context.Context, create *api.ResourceCreate)
|
|||||||
}
|
}
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
resourceRaw, err := createResource(ctx, tx, create)
|
resourceRaw, err := s.createResourceImpl(ctx, tx, create)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -109,7 +111,7 @@ func (s *Store) FindResourceList(ctx context.Context, find *api.ResourceFind) ([
|
|||||||
}
|
}
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
resourceRawList, err := findResourceList(ctx, tx, find)
|
resourceRawList, err := s.findResourceListImpl(ctx, tx, find)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -129,7 +131,7 @@ func (s *Store) FindResource(ctx context.Context, find *api.ResourceFind) (*api.
|
|||||||
}
|
}
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
list, err := findResourceList(ctx, tx, find)
|
list, err := s.findResourceListImpl(ctx, tx, find)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -172,7 +174,7 @@ func (s *Store) PatchResource(ctx context.Context, patch *api.ResourcePatch) (*a
|
|||||||
}
|
}
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
resourceRaw, err := patchResource(ctx, tx, patch)
|
resourceRaw, err := s.patchResourceImpl(ctx, tx, patch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -186,21 +188,25 @@ func (s *Store) PatchResource(ctx context.Context, patch *api.ResourcePatch) (*a
|
|||||||
return resource, nil
|
return resource, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createResource(ctx context.Context, tx *sql.Tx, create *api.ResourceCreate) (*resourceRaw, error) {
|
func (s *Store) createResourceImpl(ctx context.Context, tx *sql.Tx, create *api.ResourceCreate) (*resourceRaw, error) {
|
||||||
|
fields := []string{"filename", "blob", "external_link", "type", "size", "creator_id"}
|
||||||
|
values := []interface{}{create.Filename, create.Blob, create.ExternalLink, create.Type, create.Size, create.CreatorID}
|
||||||
|
placeholders := []string{"?", "?", "?", "?", "?", "?"}
|
||||||
|
if s.profile.IsDev() {
|
||||||
|
fields = append(fields, "visibility")
|
||||||
|
values = append(values, create.Visibility)
|
||||||
|
placeholders = append(placeholders, "?")
|
||||||
|
}
|
||||||
|
|
||||||
query := `
|
query := `
|
||||||
INSERT INTO resource (
|
INSERT INTO resource (
|
||||||
filename,
|
` + strings.Join(fields, ",") + `
|
||||||
blob,
|
|
||||||
external_link,
|
|
||||||
type,
|
|
||||||
size,
|
|
||||||
creator_id
|
|
||||||
)
|
)
|
||||||
VALUES (?, ?, ?, ?, ?, ?)
|
VALUES (` + strings.Join(placeholders, ",") + `)
|
||||||
RETURNING id, filename, blob, external_link, type, size, creator_id, created_ts, updated_ts
|
RETURNING id, ` + strings.Join(fields, ",") + `, created_ts, updated_ts
|
||||||
`
|
`
|
||||||
var resourceRaw resourceRaw
|
var resourceRaw resourceRaw
|
||||||
if err := tx.QueryRowContext(ctx, query, create.Filename, create.Blob, create.ExternalLink, create.Type, create.Size, create.CreatorID).Scan(
|
dests := []interface{}{
|
||||||
&resourceRaw.ID,
|
&resourceRaw.ID,
|
||||||
&resourceRaw.Filename,
|
&resourceRaw.Filename,
|
||||||
&resourceRaw.Blob,
|
&resourceRaw.Blob,
|
||||||
@@ -208,16 +214,19 @@ func createResource(ctx context.Context, tx *sql.Tx, create *api.ResourceCreate)
|
|||||||
&resourceRaw.Type,
|
&resourceRaw.Type,
|
||||||
&resourceRaw.Size,
|
&resourceRaw.Size,
|
||||||
&resourceRaw.CreatorID,
|
&resourceRaw.CreatorID,
|
||||||
&resourceRaw.CreatedTs,
|
}
|
||||||
&resourceRaw.UpdatedTs,
|
if s.profile.IsDev() {
|
||||||
); err != nil {
|
dests = append(dests, &resourceRaw.Visibility)
|
||||||
|
}
|
||||||
|
dests = append(dests, []interface{}{&resourceRaw.CreatedTs, &resourceRaw.UpdatedTs}...)
|
||||||
|
if err := tx.QueryRowContext(ctx, query, values...).Scan(dests...); err != nil {
|
||||||
return nil, FormatError(err)
|
return nil, FormatError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &resourceRaw, nil
|
return &resourceRaw, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func patchResource(ctx context.Context, tx *sql.Tx, patch *api.ResourcePatch) (*resourceRaw, error) {
|
func (s *Store) patchResourceImpl(ctx context.Context, tx *sql.Tx, patch *api.ResourcePatch) (*resourceRaw, error) {
|
||||||
set, args := []string{}, []interface{}{}
|
set, args := []string{}, []interface{}{}
|
||||||
|
|
||||||
if v := patch.UpdatedTs; v != nil {
|
if v := patch.UpdatedTs; v != nil {
|
||||||
@@ -226,34 +235,46 @@ func patchResource(ctx context.Context, tx *sql.Tx, patch *api.ResourcePatch) (*
|
|||||||
if v := patch.Filename; v != nil {
|
if v := patch.Filename; v != nil {
|
||||||
set, args = append(set, "filename = ?"), append(args, *v)
|
set, args = append(set, "filename = ?"), append(args, *v)
|
||||||
}
|
}
|
||||||
|
if s.profile.IsDev() {
|
||||||
|
if v := patch.Visibility; v != nil {
|
||||||
|
set, args = append(set, "visibility = ?"), append(args, *v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
args = append(args, patch.ID)
|
args = append(args, patch.ID)
|
||||||
|
|
||||||
|
fields := []string{"id", "filename", "external_link", "type", "size", "creator_id", "created_ts", "updated_ts"}
|
||||||
|
if s.profile.IsDev() {
|
||||||
|
fields = append(fields, "visibility")
|
||||||
|
}
|
||||||
|
|
||||||
query := `
|
query := `
|
||||||
UPDATE resource
|
UPDATE resource
|
||||||
SET ` + strings.Join(set, ", ") + `
|
SET ` + strings.Join(set, ", ") + `
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
RETURNING id, filename, blob, external_link, type, size, creator_id, created_ts, updated_ts
|
RETURNING ` + strings.Join(fields, ", ")
|
||||||
`
|
|
||||||
var resourceRaw resourceRaw
|
var resourceRaw resourceRaw
|
||||||
if err := tx.QueryRowContext(ctx, query, args...).Scan(
|
dests := []interface{}{
|
||||||
&resourceRaw.ID,
|
&resourceRaw.ID,
|
||||||
&resourceRaw.Filename,
|
&resourceRaw.Filename,
|
||||||
&resourceRaw.Blob,
|
|
||||||
&resourceRaw.ExternalLink,
|
&resourceRaw.ExternalLink,
|
||||||
&resourceRaw.Type,
|
&resourceRaw.Type,
|
||||||
&resourceRaw.Size,
|
&resourceRaw.Size,
|
||||||
&resourceRaw.CreatorID,
|
&resourceRaw.CreatorID,
|
||||||
&resourceRaw.CreatedTs,
|
&resourceRaw.CreatedTs,
|
||||||
&resourceRaw.UpdatedTs,
|
&resourceRaw.UpdatedTs,
|
||||||
); err != nil {
|
}
|
||||||
|
if s.profile.IsDev() {
|
||||||
|
dests = append(dests, &resourceRaw.Visibility)
|
||||||
|
}
|
||||||
|
if err := tx.QueryRowContext(ctx, query, args...).Scan(dests...); err != nil {
|
||||||
return nil, FormatError(err)
|
return nil, FormatError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &resourceRaw, nil
|
return &resourceRaw, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func findResourceList(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) ([]*resourceRaw, error) {
|
func (s *Store) findResourceListImpl(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) ([]*resourceRaw, error) {
|
||||||
where, args := []string{"1 = 1"}, []interface{}{}
|
where, args := []string{"1 = 1"}, []interface{}{}
|
||||||
|
|
||||||
if v := find.ID; v != nil {
|
if v := find.ID; v != nil {
|
||||||
@@ -273,6 +294,9 @@ func findResourceList(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) (
|
|||||||
if find.GetBlob {
|
if find.GetBlob {
|
||||||
fields = append(fields, "blob")
|
fields = append(fields, "blob")
|
||||||
}
|
}
|
||||||
|
if s.profile.IsDev() {
|
||||||
|
fields = append(fields, "visibility")
|
||||||
|
}
|
||||||
|
|
||||||
query := fmt.Sprintf(`
|
query := fmt.Sprintf(`
|
||||||
SELECT
|
SELECT
|
||||||
@@ -290,7 +314,7 @@ func findResourceList(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) (
|
|||||||
resourceRawList := make([]*resourceRaw, 0)
|
resourceRawList := make([]*resourceRaw, 0)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var resourceRaw resourceRaw
|
var resourceRaw resourceRaw
|
||||||
dest := []interface{}{
|
dests := []interface{}{
|
||||||
&resourceRaw.ID,
|
&resourceRaw.ID,
|
||||||
&resourceRaw.Filename,
|
&resourceRaw.Filename,
|
||||||
&resourceRaw.ExternalLink,
|
&resourceRaw.ExternalLink,
|
||||||
@@ -301,11 +325,12 @@ func findResourceList(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) (
|
|||||||
&resourceRaw.UpdatedTs,
|
&resourceRaw.UpdatedTs,
|
||||||
}
|
}
|
||||||
if find.GetBlob {
|
if find.GetBlob {
|
||||||
dest = append(dest, &resourceRaw.Blob)
|
dests = append(dests, &resourceRaw.Blob)
|
||||||
}
|
}
|
||||||
if err := rows.Scan(
|
if s.profile.IsDev() {
|
||||||
dest...,
|
dests = append(dests, &resourceRaw.Visibility)
|
||||||
); err != nil {
|
}
|
||||||
|
if err := rows.Scan(dests...); err != nil {
|
||||||
return nil, FormatError(err)
|
return nil, FormatError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user