-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update wort cli, expose new submit endpoint in API * compressed sigs in S3 * fix shell * status in response
- Loading branch information
Showing
8 changed files
with
152 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,64 @@ | ||
#[macro_use] | ||
extern crate clap; | ||
extern crate reqwest; | ||
#[macro_use] | ||
extern crate serde_derive; | ||
|
||
use std::error::Error; | ||
|
||
use clap::App; | ||
|
||
fn viewer(db: &str, dataset_id: &str) -> Result<(), Box<Error>> { | ||
let url = format!("https://wort.oxli.org/view/{}/{}", db, dataset_id); | ||
const BASEURL: &'static str = "https://wort.oxli.org/v1"; | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Response { | ||
status: String, | ||
} | ||
|
||
fn view(db: &str, dataset_id: &str) -> Result<(), Box<Error>> { | ||
let url = format!("{}/view/{}/{}", BASEURL, db, dataset_id); | ||
let mut res = reqwest::get(&url)?; | ||
std::io::copy(&mut res, &mut std::io::stdout())?; | ||
Ok(()) | ||
} | ||
|
||
fn main() { | ||
fn submit(db: String, dataset_id: String, token: &str, filename: &str) -> Result<(), Box<Error>> { | ||
let form = reqwest::multipart::Form::new().file("file", filename)?; | ||
|
||
let url = format!("{}/submit/{}/{}", BASEURL, db, dataset_id); | ||
|
||
let client = reqwest::Client::new(); | ||
let mut res = client | ||
.post(&url) | ||
.bearer_auth(token) | ||
.multipart(form) | ||
.send()?; | ||
println!("{}", res.json::<Response>()?.status); | ||
Ok(()) | ||
} | ||
|
||
fn main() -> Result<(), Box<Error>> { | ||
let yml = load_yaml!("wort.yml"); | ||
let m = App::from_yaml(yml).get_matches(); | ||
|
||
if let Some(cmd) = m.subcommand_matches("viewer") { | ||
viewer( | ||
cmd.value_of("database").unwrap(), | ||
cmd.value_of("dataset_id").unwrap(), | ||
); | ||
match m.subcommand_name() { | ||
Some("view") => { | ||
let cmd = m.subcommand_matches("view").unwrap(); | ||
view( | ||
cmd.value_of("database").unwrap(), | ||
cmd.value_of("dataset_id").unwrap(), | ||
) | ||
} | ||
Some("submit") => { | ||
let cmd = m.subcommand_matches("submit").unwrap(); | ||
submit( | ||
cmd.value_of("database").unwrap().into(), | ||
cmd.value_of("dataset_id").unwrap().into(), | ||
cmd.value_of("token").unwrap(), | ||
cmd.value_of("signature").unwrap(), | ||
) | ||
} | ||
None => Ok(()), // TODO: should be error | ||
_ => Ok(()), // TODO: should be error | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,39 @@ | ||
from flask import Blueprint, request, jsonify, flash | ||
import gzip | ||
from io import BytesIO | ||
import shutil | ||
|
||
from flask import Blueprint, request, jsonify, g | ||
from wort.blueprints.api.auth import token_auth | ||
|
||
|
||
submit = Blueprint("submit", __name__, template_folder="templates") | ||
|
||
|
||
@submit.route("/submit", methods=["GET", "POST"]) | ||
def submit_sigs(): | ||
if request.method == "POST": | ||
if "file" not in request.files: | ||
flash("No file part") | ||
return redirect(request.url) | ||
|
||
f = request.files["file"] | ||
print(f, request.form["public_url"]) | ||
return jsonify({}), 200 | ||
|
||
return """ | ||
<!doctype html> | ||
<title>Upload new File</title> | ||
<h1>Upload new File</h1> | ||
<form method=post enctype=multipart/form-data> | ||
<p><input type=file name=file> | ||
<input type=text name=public_url> | ||
<input type=submit value=Upload> | ||
</form> | ||
""" | ||
@token_auth.login_required | ||
def submit_sigs(public_db, dataset_id): | ||
|
||
if public_db not in ("sra", "img"): | ||
return "Database not supported", 404 | ||
|
||
import boto3 | ||
|
||
conn = boto3.client("s3") | ||
|
||
username = g.current_user.username | ||
key = f"{username}/{dataset_id}.sig" | ||
|
||
file = request.files["file"] | ||
compressed_fp = BytesIO() | ||
# TODO: if it's already gzipped, don't compress it | ||
with gzip.GzipFile(fileobj=compressed_fp, mode="wb") as gz: | ||
shutil.copyfileobj(file.stream, gz) | ||
|
||
conn.put_object( | ||
Body=compressed_fp.getvalue(), | ||
Bucket=f"wort-submitted-{public_db}", | ||
Key=key, | ||
ContentType="application/json", | ||
ContentEncoding="gzip", | ||
) | ||
|
||
return jsonify({"status": "Signature accepted"}), 202 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters