Import CID from web interface

This commit is contained in:
Citlali del Rey 2022-11-09 19:47:50 -08:00
parent 58888bd58d
commit 9eddc1d272
Signed by: nullobsi
GPG Key ID: 933A1F44222C2634
3 changed files with 92 additions and 0 deletions

View File

@ -65,6 +65,8 @@ sub startup($self) {
$r->get('/')->to('Interface#upload_get');
$r->post('/')->to('Interface#upload_post');
$r->post('/my/import')->to('Interface#import_post');
}
1;

View File

@ -230,4 +230,77 @@ sub upload_get ($c) {
return $c->render('interface/uploadPage');
}
sub import_post ($c) {
if (!IpfsUpload::Util::check_auth($c)) {
return $c->redirect_to("/login");
}
my $uid = $c->stash('uid');
my $app_name = $c->stash('app_name');
my $v = $c->validation;
return $c->render('/') unless $v->has_data;
$v->required('cid', 'trim')->size(1,128);
$v->optional('name', 'trim')->size(1,512);
$v->optional('is_browser');
return $c->render('/') if $v->has_error;
my $cid = $v->param('cid');
my $name = $v->param('name');
my $is_browser = $v->param('is_browser');
my $pub_url = Mojo::URL->new($c->config->{ipfs}->{gatewayPubUrl});
$pub_url->path($cid);
$pub_url = $pub_url->to_string;
return $c->pins->exists({
cid => $cid,
uid => $uid,
})->then(sub ($exists){
if ($exists == 1) {
if ($is_browser) {
$c->flash(msg => "Pin already exists: $pub_url");
return $c->redirect_to('/my');
} else {
return $c->render(text => $pub_url);
}
}
my $url = Mojo::URL->new($c->config->{ipfs}->{gatewayWriteUrl});
$url->path("api/v0/pin/add");
$url->query({
arg => $cid,
recursive => "true",
# It seems progress is a stream. Not sure how to handle that?
# Let's just assume it works immediately.
# TODO.
progress => "false",
});
return $c->ua->post_p($url)->then(sub($tx) {
my $res = $tx->result;
if ($res->is_success) {
# Now we do DB stuff.
return $c->pins->add({
uid => $uid,
cid => $cid,
name => $name,
app_name => $app_name,
});
} else {
# TODO: read error and use appropriate response
say $res->body;
die "Failed to pin.";
}
})->then(sub($res) {
if ($is_browser) {
$c->flash(msg => "Pin added: $pub_url");
return $c->redirect_to('/my');
} else {
return $c->render(text => $pub_url);
}
});
});
}
1;

View File

@ -20,3 +20,20 @@
%= submit_button 'Upload!'
<input hidden type="text" name="is_browser" value="Yes">
% end
<p>
Alternatively, import an existing CID:
</p>
%= form_for '/my/import' => (method => 'POST') => begin
<p>
%= label_for cid => 'CID:'
%= text_field 'cid', id => 'cid'
</p>
<p>
%= label_for name => 'Filename:'
%= text_field 'name', id => 'name'
</p>
%= submit_button 'Upload!'
<input hidden type="text" name="is_browser" value="Yes">
% end