Skip to content

Commit

Permalink
Merge branch 'uploads' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
diyor28 committed Feb 25, 2025
2 parents fafc5c4 + a6f595f commit 761d1d6
Show file tree
Hide file tree
Showing 18 changed files with 1,205 additions and 98 deletions.
31 changes: 21 additions & 10 deletions modules/core/domain/entities/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ import (

// ---- Value Objects ----

type UploadType int
type UploadType string

func (t UploadType) String() string {
return string(t)
}

const (
UploadTypeImage UploadType = iota
UploadTypeDocument
UploadTypeImage UploadType = "image"
UploadTypeDocument UploadType = "document"
)

// ---- Interfaces ----
Expand All @@ -40,7 +44,7 @@ type Upload interface {
Size() Size
IsImage() bool
PreviewURL() string
URL() url.URL
URL() *url.URL
Mimetype() *mimetype.MIME
CreatedAt() time.Time
UpdatedAt() time.Time
Expand All @@ -53,12 +57,19 @@ func New(
size int,
mimetype *mimetype.MIME,
) Upload {
var t UploadType
if strings.HasPrefix(mimetype.String(), "image") {
t = UploadTypeImage
} else {
t = UploadTypeDocument
}
return &upload{
id: 0,
hash: hash,
path: path,
size: NewSize(size),
mimetype: mimetype,
_type: t,
createdAt: time.Now(),
updatedAt: time.Now(),
}
Expand All @@ -69,6 +80,7 @@ func NewWithID(
hash, path string,
size int,
mimetype *mimetype.MIME,
_type UploadType,
createdAt, updatedAt time.Time,
) Upload {
return &upload{
Expand All @@ -77,6 +89,7 @@ func NewWithID(
path: path,
size: NewSize(size),
mimetype: mimetype,
_type: _type,
createdAt: createdAt,
updatedAt: updatedAt,
}
Expand All @@ -87,6 +100,7 @@ type upload struct {
hash string
path string
size Size
_type UploadType
mimetype *mimetype.MIME
createdAt time.Time
updatedAt time.Time
Expand All @@ -97,10 +111,7 @@ func (u *upload) ID() uint {
}

func (u *upload) Type() UploadType {
if strings.HasPrefix(u.mimetype.String(), "image") {
return UploadTypeImage
}
return UploadTypeDocument
return u._type
}

func (u *upload) Hash() string {
Expand All @@ -115,9 +126,9 @@ func (u *upload) Size() Size {
return u.size
}

func (u *upload) URL() url.URL {
func (u *upload) URL() *url.URL {
conf := configuration.Use()
return url.URL{
return &url.URL{
Scheme: conf.Scheme(),
Host: conf.Domain,
Path: u.path,
Expand Down
21 changes: 16 additions & 5 deletions modules/core/domain/entities/upload/upload_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package upload
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"path/filepath"

"github.com/gabriel-vasile/mimetype"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/net/context"

ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
"github.com/iota-uz/iota-sdk/pkg/configuration"
"github.com/iota-uz/iota-sdk/pkg/constants"
Expand All @@ -18,20 +20,29 @@ type CreateDTO struct {
File io.ReadSeeker `validate:"required"`
Name string `validate:"required"`
Size int `validate:"required"`
Type string
}

func (d *CreateDTO) Ok(l ut.Translator) (map[string]string, bool) {
func (d *CreateDTO) Ok(ctx context.Context) (map[string]string, bool) {
l, ok := ctx.Value(constants.LocalizerKey).(*i18n.Localizer)
if !ok {
panic("localizer not found in context")
}
errorMessages := map[string]string{}
errs := constants.Validate.Struct(d)
if errs == nil {
return errorMessages, true
}

for _, err := range errs.(validator.ValidationErrors) {
errorMessages[err.Field()] = err.Translate(l)
errorMessages[err.Field()] = l.MustLocalize(&i18n.LocalizeConfig{
MessageID: fmt.Sprintf("ValidationErrors.%s", err.Tag()),
TemplateData: map[string]string{
"Field": err.Field(),
},
})
}

return errorMessages, len(errorMessages) == 0

}

func (d *CreateDTO) ToEntity() (Upload, []byte, error) {
Expand Down
17 changes: 10 additions & 7 deletions modules/core/domain/entities/upload/upload_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ package upload

import (
"context"

"github.com/gabriel-vasile/mimetype"
)

type FindParams struct {
ID uint
Hash string
Limit int
Offset int
SortBy []string
Search string
Type string
ID uint
Hash string
Limit int
Offset int
SortBy []string
Search string
Type UploadType
Mimetype *mimetype.MIME
}

type Repository interface {
Expand Down
4 changes: 4 additions & 0 deletions modules/core/gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,7 @@ models:
resolver: true
total:
resolver: true
File:
model:
- github.com/99designs/gqlgen/graphql.Upload

2 changes: 2 additions & 0 deletions modules/core/infrastructure/persistence/core_mappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func ToDBUpload(upload upload.Upload) *models.Upload {
Path: upload.Path(),
Hash: upload.Hash(),
Size: upload.Size().Bytes(),
Type: upload.Type().String(),
Mimetype: upload.Mimetype().String(),
CreatedAt: upload.CreatedAt(),
UpdatedAt: upload.UpdatedAt(),
Expand All @@ -161,6 +162,7 @@ func ToDomainUpload(dbUpload *models.Upload) upload.Upload {
dbUpload.Path,
dbUpload.Size,
mime,
upload.UploadType(dbUpload.Type),
dbUpload.CreatedAt,
dbUpload.UpdatedAt,
)
Expand Down
2 changes: 1 addition & 1 deletion modules/core/infrastructure/persistence/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Upload struct {
Path string
Size int
Mimetype string
Type string
CreatedAt time.Time
UpdatedAt time.Time
}
Expand Down Expand Up @@ -88,7 +89,6 @@ type UploadedImage struct {
Height int
CreatedAt time.Time
UpdatedAt time.Time
Upload Upload
}

type Session struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ CREATE TABLE companies
CREATE TABLE uploads
(
id SERIAL PRIMARY KEY,
hash VARCHAR(255) NOT NULL UNIQUE,
path VARCHAR(1024) NOT NULL DEFAULT '',
size INT NOT NULL DEFAULT 0,
mimetype VARCHAR(255) NOT NULL,
hash VARCHAR(255) NOT NULL UNIQUE, -- md5 hash of the file
path VARCHAR(1024) NOT NULL DEFAULT '', -- relative path to the file
size INT NOT NULL DEFAULT 0, -- in bytes
mimetype VARCHAR(255) NOT NULL, -- image/jpeg, application/pdf, etc.
type VARCHAR(255) NOT NULL, -- image, document, etc.
created_at TIMESTAMP WITH TIME ZONE DEFAULT current_timestamp,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT current_timestamp
);
Expand Down
6 changes: 5 additions & 1 deletion modules/core/infrastructure/persistence/upload_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ func (g *GormUploadRepository) GetPaginated(
}

if params.Type != "" {
where, args = append(where, fmt.Sprintf("mimetype = $%d", len(args)+1)), append(args, params.Type)
where, args = append(where, fmt.Sprintf("type = $%d", len(args)+1)), append(args, params.Type.String())
}

if params.Mimetype != nil {
where, args = append(where, fmt.Sprintf("mimetype = $%d", len(args)+1)), append(args, params.Mimetype.String())
}

rows, err := pool.Query(ctx, `
Expand Down
11 changes: 10 additions & 1 deletion modules/core/interfaces/graph/base.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
scalar Time
scalar Int64

directive @goModel(
model: String
models: [String!]
) on OBJECT | INPUT_OBJECT | SCALAR | ENUM | INTERFACE | UNION

directive @goEnum(
value: String
) on ENUM_VALUE

type Query {
hello(name: String): String
}
Expand All @@ -11,4 +20,4 @@ type Mutation {

type Subscription {
counter: Int!
}
}
Loading

0 comments on commit 761d1d6

Please sign in to comment.