Skip to content

Commit

Permalink
refactor: Removing captain obvious comments
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Aug 15, 2022
1 parent bcc30fa commit 9d9344e
Show file tree
Hide file tree
Showing 26 changed files with 23 additions and 162 deletions.
6 changes: 3 additions & 3 deletions cmd/fibr/fibr.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ func main() {
shareApp, err := share.New(shareConfig, storageProvider, amqpClient)
logger.Fatal(err)

amqpExifApp, err := amqphandler.New(amqpExifConfig, amqpClient, exifApp.AmqpHandler)
amqpExifApp, err := amqphandler.New(amqpExifConfig, amqpClient, exifApp.AMQPHandler)
logger.Fatal(err)

amqpShareApp, err := amqphandler.New(amqpShareConfig, amqpClient, shareApp.AmqpHandler)
amqpShareApp, err := amqphandler.New(amqpShareConfig, amqpClient, shareApp.AMQPHandler)
logger.Fatal(err)

amqpWebhookApp, err := amqphandler.New(amqpWebhookConfig, amqpClient, webhookApp.AmqpHandler)
amqpWebhookApp, err := amqphandler.New(amqpWebhookConfig, amqpClient, webhookApp.AMQPHandler)
logger.Fatal(err)

crudApp, err := crud.New(crudConfig, storageProvider, rendererApp, shareApp, webhookApp, thumbnailApp, exifApp, eventBus.Push, amqpClient, tracerApp)
Expand Down
1 change: 0 additions & 1 deletion pkg/crud/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/ViBiOh/httputils/v4/pkg/renderer"
)

// Create creates given path directory to filesystem
func (a App) Create(w http.ResponseWriter, r *http.Request, request provider.Request) {
if !request.CanEdit {
a.error(w, r, request, model.WrapForbidden(ErrNotAuthorized))
Expand Down
18 changes: 3 additions & 15 deletions pkg/crud/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,12 @@ import (
)

var (
// ErrNotAuthorized error returned when user is not authorized
ErrNotAuthorized = errors.New("you're not authorized to do this ⛔")

// ErrEmptyName error returned when user does not provide a name
ErrEmptyName = errors.New("name is empty")

// ErrEmptyFolder error returned when user does not provide a folder
ErrEmptyFolder = errors.New("folder is empty")

// ErrAbsoluteFolder error returned when user provide a relative folder
ErrNotAuthorized = errors.New("you're not authorized to do this ⛔")
ErrEmptyName = errors.New("name is empty")
ErrEmptyFolder = errors.New("folder is empty")
ErrAbsoluteFolder = errors.New("folder has to be absolute")
)

// App of package
type App struct {
tracer trace.Tracer
rawStorageApp absto.Storage
Expand All @@ -58,7 +50,6 @@ type App struct {
chunkUpload bool
}

// Config of package
type Config struct {
ignore *string
amqpExclusiveRoutingKey *string
Expand All @@ -68,7 +59,6 @@ type Config struct {
chunkUpload *bool
}

// Flags adds flags for configuring package
func Flags(fs *flag.FlagSet, prefix string) Config {
return Config{
ignore: flags.String(fs, prefix, "crud", "IgnorePattern", "Ignore pattern when listing files or directory", "", nil),
Expand All @@ -82,7 +72,6 @@ func Flags(fs *flag.FlagSet, prefix string) Config {
}
}

// New creates new App from Config
func New(config Config, storage absto.Storage, rendererApp renderer.App, shareApp provider.ShareManager, webhookApp provider.WebhookManager, thumbnailApp thumbnail.App, exifApp exif.App, eventProducer provider.EventProducer, amqpClient *amqp.Client, tracerApp tracer.App) (App, error) {
app := App{
sanitizeOnStart: *config.sanitizeOnStart,
Expand Down Expand Up @@ -151,7 +140,6 @@ func New(config Config, storage absto.Storage, rendererApp renderer.App, shareAp
return app, nil
}

// Start crud operations
func (a App) Start(done <-chan struct{}) {
if a.amqpClient == nil {
a.start(context.Background(), done)
Expand Down
1 change: 0 additions & 1 deletion pkg/crud/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func parseMultipart(r *http.Request) (map[string]string, *multipart.Part, error)
}
}

// Post handle post from form
func (a App) Post(w http.ResponseWriter, r *http.Request, request provider.Request) {
contentType := r.Header.Get("Content-Type")

Expand Down
1 change: 0 additions & 1 deletion pkg/crud/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func (a App) doRename(ctx context.Context, oldPath, newPath string, oldItem abst
return newItem, nil
}

// Rename rename given path to a new one
func (a App) Rename(w http.ResponseWriter, r *http.Request, request provider.Request) {
if !request.CanEdit {
a.error(w, r, request, model.WrapForbidden(ErrNotAuthorized))
Expand Down
4 changes: 0 additions & 4 deletions pkg/exif/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func redisKey(itemID string) string {
return fmt.Sprintf("fibr:%s:exif:%s", cacheVersion, itemID)
}

// GetExifFor return exif value for a given item
func (a App) GetExifFor(ctx context.Context, item absto.Item) (exas.Exif, error) {
if item.IsDir {
return exas.Exif{}, nil
Expand All @@ -45,7 +44,6 @@ func (a App) GetExifFor(ctx context.Context, item absto.Item) (exas.Exif, error)
}, cacheDuration)
}

// GetAggregateFor return aggregated value for a given directory
func (a App) GetAggregateFor(ctx context.Context, item absto.Item) (provider.Aggregate, error) {
if !item.IsDir {
return provider.Aggregate{}, nil
Expand All @@ -64,12 +62,10 @@ func (a App) GetAggregateFor(ctx context.Context, item absto.Item) (provider.Agg
}, cacheDuration)
}

// SaveExifFor saves given exif for given item
func (a App) SaveExifFor(ctx context.Context, item absto.Item, exif exas.Exif) error {
return cache.EvictOnSuccess(ctx, a.redisClient, redisKey(item.ID), a.saveMetadata(ctx, item, exif))
}

// SaveAggregateFor saves given aggregate for given item
func (a App) SaveAggregateFor(ctx context.Context, item absto.Item, aggregate provider.Aggregate) error {
return cache.EvictOnSuccess(ctx, a.redisClient, redisKey(item.ID), a.saveMetadata(ctx, item, aggregate))
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/exif/amqp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
"github.com/streadway/amqp"
)

// AmqpHandler handle exif message
func (a App) AmqpHandler(message amqp.Delivery) error {
func (a App) AMQPHandler(message amqp.Delivery) error {
var resp provider.ExifResponse

if err := json.Unmarshal(message.Body, &resp); err != nil {
Expand Down
2 changes: 0 additions & 2 deletions pkg/exif/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/ViBiOh/httputils/v4/pkg/logger"
)

// EventConsumer handle event pushed to the event bus
func (a App) EventConsumer(ctx context.Context, e provider.Event) {
if !a.enabled() {
return
Expand Down Expand Up @@ -45,7 +44,6 @@ func (a App) EventConsumer(ctx context.Context, e provider.Event) {
}
}

// Rename exif of an item
func (a App) Rename(ctx context.Context, old, new absto.Item) error {
if err := a.storageApp.Rename(ctx, Path(old), Path(new)); err != nil && !absto.IsNotExist(err) {
return fmt.Errorf("rename exif: %s", err)
Expand Down
5 changes: 0 additions & 5 deletions pkg/exif/exif.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"go.opentelemetry.io/otel/trace"
)

// App of package
type App struct {
tracer trace.Tracer
storageApp absto.Storage
Expand All @@ -42,7 +41,6 @@ type App struct {
directAccess bool
}

// Config of package
type Config struct {
exifURL *string
exifUser *string
Expand All @@ -55,7 +53,6 @@ type Config struct {
directAccess *bool
}

// Flags adds flags for configuring package
func Flags(fs *flag.FlagSet, prefix string) Config {
return Config{
exifURL: flags.String(fs, prefix, "exif", "URL", "Exif Tool URL (exas)", "http://exas:1080", nil),
Expand All @@ -70,7 +67,6 @@ func Flags(fs *flag.FlagSet, prefix string) Config {
}
}

// New creates new App from Config
func New(config Config, storageApp absto.Storage, prometheusRegisterer prometheus.Registerer, tracerApp tracer.App, amqpClient *amqpclient.Client, redisClient redis.App) (App, error) {
var amqpExchange string
if amqpClient != nil {
Expand Down Expand Up @@ -103,7 +99,6 @@ func New(config Config, storageApp absto.Storage, prometheusRegisterer prometheu
}, nil
}

// ListDir return all exifs for a given directory
func (a App) ListDir(ctx context.Context, item absto.Item) ([]absto.Item, error) {
if !item.IsDir {
return nil, nil
Expand Down
2 changes: 0 additions & 2 deletions pkg/exif/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import (
"github.com/ViBiOh/fibr/pkg/provider"
)

// CanHaveExif determine if exif can be extracted for given pathname
func (a App) CanHaveExif(item absto.Item) bool {
return provider.ThumbnailExtensions[item.Extension] && (a.maxSize == 0 || item.Size < a.maxSize || a.directAccess)
}

// Path computes exif path for a a given item
func Path(item absto.Item) string {
if item.IsDir {
return provider.MetadataDirectory(item) + "aggregate.json"
Expand Down
9 changes: 3 additions & 6 deletions pkg/fibr/fibr.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ import (
"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/httputils/v4/pkg/logger"
"github.com/ViBiOh/httputils/v4/pkg/model"
httpModel "github.com/ViBiOh/httputils/v4/pkg/model"
"github.com/ViBiOh/httputils/v4/pkg/renderer"
)

// App of package
type App struct {
loginApp provider.Auth
crudApp provider.Crud
Expand All @@ -27,7 +25,6 @@ type App struct {
rendererApp renderer.App
}

// New creates new App from Config
func New(crudApp provider.Crud, rendererApp renderer.App, shareApp provider.ShareManager, webhookApp provider.WebhookManager, loginApp provider.Auth) App {
return App{
crudApp: crudApp,
Expand Down Expand Up @@ -140,14 +137,14 @@ func (a App) parseShare(request *provider.Request, authorizationHeader string) e

func convertAuthenticationError(err error) error {
if errors.Is(err, auth.ErrForbidden) {
return httpModel.WrapForbidden(errors.New("you're not authorized to speak to me with this terms"))
return model.WrapForbidden(errors.New("you're not authorized to speak to me with this terms"))
}

if errors.Is(err, ident.ErrMalformedAuth) {
return httpModel.WrapInvalid(err)
return model.WrapInvalid(err)
}

return httpModel.WrapUnauthorized(err)
return model.WrapUnauthorized(err)
}

func logRequest(r *http.Request) {
Expand Down
10 changes: 2 additions & 8 deletions pkg/geo/geo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
)

// Type is a type of GeoJSON Object
// Type is a type of GeoJSON Object.
type Type string

const (
Expand All @@ -14,14 +14,12 @@ const (
TypePoint Type = "Point"
)

// Feature description
type Feature struct {
Properties map[string]any `json:"properties"`
Geometry any `json:"geometry"`
Type Type `json:"type"`
}

// NewFeature creates a Feature from given geometry and properties
func NewFeature(geometry any, properties map[string]any) Feature {
return Feature{
Type: TypeFeature,
Expand All @@ -30,35 +28,31 @@ func NewFeature(geometry any, properties map[string]any) Feature {
}
}

// Point description
type Point struct {
Type Type `json:"type"`
Coordinates Position `json:"coordinates"`
}

// NewPoint creates a Point from given position
func NewPoint(position Position) Point {
return Point{
Type: TypePoint,
Coordinates: position,
}
}

// Position description
type Position struct {
Longitude float64
Latitude float64
}

// NewPosition creates a new position
func NewPosition(lon, lat float64) Position {
return Position{
Longitude: lon,
Latitude: lat,
}
}

// MarshalJSON marshals the position as an array
// MarshalJSON marshals the position as an array.
func (p Position) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("[%f,%f]", p.Longitude, p.Latitude)), nil
}
Loading

0 comments on commit 9d9344e

Please sign in to comment.