Skip to content
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

Make Mojo::Asset::File writable to the existed files. #649

Closed
wants to merge 3 commits into from

Conversation

iakuf
Copy link

@iakuf iakuf commented Jul 21, 2014

After debugging for days, I figured it out finally and fixed my code.
This patch is to make the Mojo::Asset::File more flexible, it gives the write permission to the module when handling uploading/downloading files.

@kraih
Copy link
Member

kraih commented Jul 21, 2014

What's the use case for this change?

@iakuf
Copy link
Author

iakuf commented Jul 21, 2014

Actually, when uploading a huge file(eg. 2GB), I hope Mojo::Asset::File to be able to upload the file in multiply HTTP requests. In this way, people won't have to wait the uploading process for hours, instead, they can resume the upload process at any time they want.

I found the "add_chunk" in Mojo::Asset::File is able to do it. Unfortunately, "add_chunk" can only add "chunk" to the file it's uploading. It's not able to add chunk to an existed file, which is required for my use case.

So my proposal is to only change one bit of Mojo::Asset::File module at line 20. It will help a lot for my scenario and it doesn't affect any existed features.

@kraih
Copy link
Member

kraih commented Jul 21, 2014

Could you elaborate a bit? I've brought up the topic on IRC and we were having trouble understanding the use case.

@@ -209,6 +209,18 @@ ok !$asset->is_file, 'stored in memory';
ok !-e $path, 'file has been cleaned up';
}

# Custom existing file
my ($fh, $tmppath) = tempfile();
Copy link
Member

Choose a reason for hiding this comment

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

$fh is initialized but not used.

@iakuf
Copy link
Author

iakuf commented Jul 21, 2014

I was in China, can not connect to the IRC server.

@iakuf
Copy link
Author

iakuf commented Jul 21, 2014

I am here to develop a HTML5 file upload to a binary file into a way to upload the data block .
Client upload 1M, it will return the current size per upload to upload, and then disconnects.After re-create the connection from the position of the file transfer completed last start transferring files.
So I have come to the file related operations by Mojo::Asset::File module. in this way, users can upload breakpoint resume. the process can be interrupted at any, some time after resuming.

@iakuf
Copy link
Author

iakuf commented Jul 21, 2014

Much like the following, each connection, open a file, and then write additional blocks.
hook after_build_tx => sub {
my $tx = shift;
my $app = shift;
weaken $tx;

$tx->req->content->on(body => sub {
    my $single  = shift;
    return unless $tx->req->method eq 'POST' or $tx->req->method eq 'OPTIONS';
    return unless $tx->req->url->path->contains('/upload');

    my $file = Mojo::Asset::File->new(path => $tx->req->param("path"));
    $single->unsubscribe('read')->on(read => sub {
        my ($single, $bytes) = @_; 
        $file->add_chunk($bytes);
    }); 
}); 

};
After completion of the transfer will return the size of 1M.
any [qw(POST OPTIONS GET)] => '/upload' => sub {
my $self = shift;
my $path = $self->param('path');

my $filesize = -f $path ? -s $path : 0 ;
return $self->render(json => {
    start   => $filesize,
    success => 1,
    message => '',
});

};
If you have not finished 1M transfer, only passing a portion of 1M, this "get / upload" interfaces can also get the results of the current size then the client can then resume at that point.

@iakuf
Copy link
Author

iakuf commented Jul 21, 2014

We're sorry, the second paragraph of the code is wrong.

@kraih kraih closed this in 5facbf6 Jul 21, 2014
@kraih
Copy link
Member

kraih commented Jul 21, 2014

Thanks, applied with a few small changes.

@iakuf
Copy link
Author

iakuf commented Jul 23, 2014

Thank you very much for supporting me, my html5 large files "breakpoint resume" developed using the latest Mojolicous finished.

My project addresses: https://github.com/iakuf/mojolicious-stream-upload

@kraih
Copy link
Member

kraih commented Oct 18, 2014

I'm afraid this is going to be reverted in Mojolicious 5.52, because there have been too many problems with read-only file systems. Lots of test failures from CPAN Testers and user complaints. 04aa323

@kiwiroy
Copy link
Contributor

kiwiroy commented Oct 19, 2014

Is there any support for using utime(undef, undef, $path) in place of -w _ as a means of ascertaining write-able in that test?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants