-
-
Notifications
You must be signed in to change notification settings - Fork 529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add receive feature #74
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you agree, please address issues below. After that I have more comments about how the code is structured, but some issues need to be addressed first. Thanks!
main.go
Outdated
@@ -17,37 +17,45 @@ var forceFlag = flag.Bool("force", false, "ignore saved configuration") | |||
var debugFlag = flag.Bool("debug", false, "increase verbosity") | |||
var quietFlag = flag.Bool("quiet", false, "ignores non critical output") | |||
var portFlag = flag.Int("port", 0, "port to bind the server to") | |||
var receiveMode = flag.Bool("recieve", false, "allows to receive files") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- please replace with
receiveFlag
, for consistency - please fix typo
recieve
->receive
- please reword the description to "receives files"
} | ||
serveFilesHTTP(generatedAddress, route, content, wg, stopSignal) | ||
|
||
defer func() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure why this function call should be deferred. Any hint?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously, if the program argument was a directory, the directory was compressed. In order to avoid unnecessary computation, the getContent()
call is performed only when the program is supposed to serve files. But at the same time, the content should be deleted under certain conditions and not earlier than the server will finish its work. Because of this, the deletion must be done last. For such purposes, defer is used.
serveHTTP.go
Outdated
} | ||
} | ||
// create output file if error was handled succesfully | ||
out, _ = os.Create(filepath.Join(dirToStore, fileHeader.Filename)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check error here
serveHTTP.go
Outdated
stop <- true // send signal to server to shutdown | ||
} | ||
// create output file if error was handled succesfully | ||
out, _ = os.Create(filepath.Join(dirToStore, fileHeader.Filename)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check error here as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All errors handled above. At this point there will be no error
serveHTTP.go
Outdated
|
||
// try to create output file | ||
out, err := os.Create(filepath.Join(dirToStore, fileHeader.Filename)) | ||
defer out.Close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move defer out.Close()
after checking the error
serveHTTP.go
Outdated
// try to create output file | ||
out, err := os.Create(filepath.Join(dirToStore, fileHeader.Filename)) | ||
defer out.Close() | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For sake of readability, please complement the behavior here: if err == nil
, then return/abort/cleanup or do something else.
serveHTTP.go
Outdated
defer out.Close() | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
err := os.MkdirAll(dirToStore, os.ModePerm) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be refactored:
if err := os.MkdirAll(.....); err != nil {
// fmt.fprintf... unable to create specified dir....
}
Also, double check spelling please.
serveHTTP.go
Outdated
out, _ = os.Create(filepath.Join(dirToStore, fileHeader.Filename)) | ||
|
||
// write the content from POSTed file to the out | ||
_, err = io.Copy(out, file) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified:
if _, err := io.Copy(out, file); err != nil {
// unable to write file to disk....
}
serveHTTP.go
Outdated
stop <- true // send signal to server to shutdown | ||
} | ||
|
||
fmt.Fprintf(w, "File uploaded successfully: %s\n", fileHeader.Filename) //ouput to server |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is reached even if err != nil
, resulting in confusing message
More info: rename variable name "receiveMode" to "receiveFlag" fix typo in flag's name change flag's description
Sorry I accidentally clicked on "approve changes" and I cannot undo the action for some reason :-/ |
@claudiodangelis What should the program do if the user uploads a file with a name that is already taken. Maybe change the name of a new file like this |
Very good question. I like the suffix idea, I think that should be the
behavior in case of already existing file.
Il giorno mer 8 ago 2018 alle ore 18:56 Alexandr Bruyako <
notifications@github.com> ha scritto:
… @claudiodangelis <https://github.com/claudiodangelis> What should the
program do if the user uploads a file with a name that is already taken.
Maybe change the name of a new file like this photo.jpg -> photo(1).jpg
and notify a user about that. Or should be this done some other way?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#74 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AA5fixMFkS4_GWVq9pATvYea3k6KEQYmks5uOxg6gaJpZM4Vzs1M>
.
|
Maybe, once the transfer is complete, we just print the name of the
downloaded file, either it existed or not.
Il giorno mer 8 ago 2018 alle ore 18:58 Claudio d'Angelis <
claudiodangelis@gmail.com> ha scritto:
… Very good question. I like the suffix idea, I think that should be the
behavior in case of already existing file.
Il giorno mer 8 ago 2018 alle ore 18:56 Alexandr Bruyako <
***@***.***> ha scritto:
> @claudiodangelis <https://github.com/claudiodangelis> What should the
> program do if the user uploads a file with a name that is already taken.
> Maybe change the name of a new file like this photo.jpg -> photo(1).jpg
> and notify a user about that. Or should be this done some other way?
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <#74 (comment)>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AA5fixMFkS4_GWVq9pATvYea3k6KEQYmks5uOxg6gaJpZM4Vzs1M>
> .
>
|
@claudiodangelis I see it like that: if the user uploads a file with a name that is already taken filename changes to |
I see it similarly: we define a "filename" variable set to the uploading file. If the uploading filename already exists, we change the variable name by adding a suffix. At the end of the process, we print the filename variable which may or may not have been edited since the initial definition. |
I'm leaving my desk now, please expect delayed responses. 🌚 |
@claudiodangelis Have you seen last changes? |
Yep, it works great, many thanks for this. I think there is still room for improvement though, please allow me some time to do a second review. |
For example, what about renaming the flag to As of now what happens is extremely confusing because there is no indication to the user of what the first argument will be used for if the |
@claudiodangelis In the majority of programs, the flags reflect not the specific behavior of the program, but the concept of what it will do (extract, help, recursive, and so on). Perhaps for consistency it's worth to leave just |
Yes, completely agree with you, Additional args should be ignored, for example in the case of: qr-filetransfer -receive /path/to/something the file should be transfered to the current working directory, not to What do you think? |
I do not really like the idea of defining a directory as a separate flag. It is better to leave the current version, when the destination directory is specified after the |
Ok, I'm not 100% convinced but let's try this way. Does it currently handle
both absolute and relative paths?
Il lun 13 ago 2018, 18:09 Alexandr Bruyako <notifications@github.com> ha
scritto:
… I do not really like the idea of defining a directory as a separate flag.
It is better to leave the current version, when the destination directory
is specified after the flag. And in the case when the user does not specify
a directory, displays the message At least one argument is required. In
case you want to save the file in the working directory, you can simply
call the program like this qr-filetransfer -receive .
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#74 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AA5fi7jBwZprOD34ymFtUPphBkJ92ugUks5uQaTMgaJpZM4Vzs1M>
.
|
Yes, both relative and absolute. Tested only on Linux, but I think there will not be problems on other operating systems, because only standard library functions were used and none of "system-specific hacks" |
Meanwhile the project got a logo (arasatasaygin/openlogos#4), I'm awaiting logo's assets so we can include it in the upload page. |
I will improve the html tomorrow, include the logo and merge to master 💪 |
Merged, thank you @brualan! |
@claudiodangelis Awesome! Can you please update README, to let people know how to use this feature? |
More info: #58