Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Thumbnails Service #3

Merged
merged 22 commits into from
Mar 18, 2020
Merged

Thumbnails Service #3

merged 22 commits into from
Mar 18, 2020

Conversation

C0rby
Copy link
Contributor

@C0rby C0rby commented Mar 10, 2020

Can generate thumbnails and store them in the filesystem.

Still missing:

Example request:

GET /thumbnails?file_path=oc.png&width=500&height=300&type=png&etag=979f4c8db98f7b82e768ef478d3c8612 HTTP/1.1
> Host: localhost:9190
> User-Agent: curl/7.69.0
> Accept: */*
> Authorization: Bearer <JWT>

@update-docs
Copy link

update-docs bot commented Mar 10, 2020

Thanks for opening this pull request! The maintainers of this repository would appreciate it if you would create a changelog item based on your changes.

@C0rby C0rby requested review from butonic and refs March 10, 2020 14:23
w.Write([]byte("Hello ocis-thumbnails!"))
encoder := thumbnails.EncoderForType(fileType)
if encoder == nil {
// TODO: better error responses
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use https://golang.org/pkg/net/http/#Error here and in all subsequent cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. But since the API will be changed in another PR anyways I would leave it like that for now.

Copy link
Member

@refs refs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌 a bit of nitty-gritty regarding functionality and style. Other than that looks good. Still need to run it locally tho.

@@ -89,7 +89,7 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
},
&cli.StringFlag{
Name: "debug-addr",
Value: "0.0.0.0:9114",
Value: "0.0.0.0:9194",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure to reserve a port range here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I wanted to change that back... But yes we should define a port for the service.

// NewFileSystemStorage creates a new instanz of FileSystem
func NewFileSystemStorage(cfg config.FileSystemStorage) FileSystem {
return FileSystem{
dir: cfg.RootDirectory,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this work on non UNIX systems?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd leave UNIX by default and on a Before hook check the host OS and update config

func (s FileSystem) Set(key string, img []byte) error {
path := filepath.Join(s.dir, key)
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0700); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it need execution permissions? or can this be set to 0600?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, execution permission is needed to be able to read or write files in the directory.


f, err := os.Create(path)
if err != nil {
fmt.Println(err.Error())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a proper logger

}

m.Route(options.Config.HTTP.Root, func(r chi.Router) {
r.Get("/", svc.Dummy)
r.Get("/thumbnails", svc.Thumbnails)
})

return svc
}

// Thumbnails defines implements the business logic for Service.
type Thumbnails struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change annotation to:

Thumbnails implements the business logic for Service.

Also use the singular, as in Thumbnail 💃

Comment on lines 59 to 60
width, _ := strconv.Atoi(query.Get("width"))
height, _ := strconv.Atoi(query.Get("height"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add error handling here, make it more explicit where a failure might happen

height, _ := strconv.Atoi(query.Get("height"))
fileType := query.Get("type")
filePath := query.Get("file_path")
etag := query.Get("etag")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally speaking I'd declare a struct and try to unmarshal the request query string onto it, then operate with typed fields. It is the same concept you're doing here but a bit more explicit. We could give it a try together

if encoder == nil {
// TODO: better error responses
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("can't encode that"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a logger from ocis-pkg so we have persisted logs and not only the user can see it. I'm also fond of using the passive voice, as in:

can't be encoded

Furthermore the error message is not informative enough, since you're checking whether the fileType is supported or not by fetching an encoder for it, it should read more like:

encoding for filetype: .XYZ is not supported

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to replace that when the proper API is implemented.

}

// NewContext creates a new SourceContext instance
func NewContext() SourceContext {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you considered using context.Context here? You can still store key-values on a typed and thread safe manner 🗡

}

func mapToStorageContext(ctx Context) storage.Context {
sCtx := storage.Context{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as with the previous context.Context comment. By adopting that idiom you could even chain them like middlewares

@refs refs changed the title First working version. Thumbnails Service Mar 11, 2020
@refs refs added the enhancement New feature or request label Mar 11, 2020
@refs
Copy link
Member

refs commented Mar 11, 2020

Also don't forget to add a changelog 💃

@C0rby C0rby requested review from IljaN and refs March 11, 2020 15:15
@C0rby
Copy link
Contributor Author

C0rby commented Mar 11, 2020

The only things left now are the things I want to change in another PR when I'll implement the proper API. Or should I do it here?

@C0rby
Copy link
Contributor Author

C0rby commented Mar 13, 2020

So now I added the grpc service. @refs could you have another look?

Copy link
Member

@refs refs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Thanks for adding traces and metrics!

Comment on lines +38 to +41
} else {
logger.Debug().
Msg("")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leftover? or intended to populate this with a message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I copied it from ocis-hello.
I thought it would then log at least the stuff from line 30 and 31

@C0rby C0rby merged commit 96e3ec5 into master Mar 18, 2020
@delete-merged-branch delete-merged-branch bot deleted the thumbnail_service branch March 18, 2020 13:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants