Crate waifuvault

Source
Expand description

This is the Rust version of the Waifu Vault SDK which is used to interact with the file upload service.

For Terms of Service and usage policy, please refer to the above website.

§Uploading a file

use waifuvault::{
    ApiCaller,
    api::{WaifuUploadRequest}
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let caller = ApiCaller::new();

    // Upload a file from disk
    let request = WaifuUploadRequest::new()
        .file("/some/file/path") // Path to a file
        .password("set a password") // Set a password
        .one_time_download(true); // Delete after first access
    let response = caller.upload_file(request).await?;

    // Upload a file from a URL
    let request = WaifuUploadRequest::new()
        .url("https://some-website/image.jpg"); // URL to content
    let response = caller.upload_file(request).await?;

    // Upload a file from raw bytes
    let data = std::fs::read("some/file/path")?;
    let request = WaifuUploadRequest::new()
        .bytes(data, "name-to-store.rs"); // Raw file content and name to store on the vault
    let response = caller.upload_file(request).await?;

    Ok(())
}

§Get File Information

use waifuvault::{
    ApiCaller,
    api::WaifuGetRequest
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let caller = ApiCaller::new();

    let request = WaifuGetRequest::new("some-waifu-vault-token");
    let response = caller.file_info(request).await?;

    // Do something with the response

    Ok(())
}

§Modify Existing File Properties

use waifuvault::{
    ApiCaller,
    api::WaifuModificationRequest
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let caller = ApiCaller::new();

    let request = WaifuModificationRequest::new("some-waifu-vault-token")
        .password("new_password") // Set a new password
        .previous_password("old_password") // Old password
        .custom_expiry("1h") // Set a new expiry
        .hide_filename(true); // Hide the filename

    let response = caller.update_file(request).await?;

    // Do something with the response

    Ok(())
}

§Delete a file from Waifu Vault

use waifuvault::ApiCaller;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let caller = ApiCaller::new();
    let response = caller.delete_file("some-waifu-token").await?;

    Ok(())
}

§Download a file

use waifuvault::ApiCaller;
use std::io::Write;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let caller = ApiCaller::new();

    // Download a file with no password
    let content = caller.download_file("https://waifuvault.moe/f/some-file.ext", None).await?;
    let mut f = std::fs::File::create("downloaded_file.txt")?;
    f.write_all(&content)?;

    // Download a file with no password
    let content = caller.download_file("https://waifuvault.moe/f/some-other-file.ext", Some("password".to_string())).await?;
    let mut f = std::fs::File::create("downloaded_file2.txt")?;
    f.write_all(&content)?;

    Ok(())
}

§Create a Bucket

use waifuvault::{ApiCaller, api::WaifuUploadRequest};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let caller = ApiCaller::new();

    // Create a new bucket to upload files to
    let bucket = caller.create_bucket().await?;

    // You can now use the bucket token to upload files to the bucket

    let request = WaifuUploadRequest::new()
        .file("/some/file/path")
        .bucket(&bucket.token)
        .password("set a password")
        .one_time_download(true);
    let response = caller.upload_file(request).await?;

    // Do something with the response

    Ok(())
}

§Delete a Bucket

use waifuvault::ApiCaller;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let caller = ApiCaller::new();

    let token = "some-bucket-token";

    // Delete the bucket and all files within
    caller.delete_bucket(token).await?;

    Ok(())
}

§Get Bucket information

use waifuvault::ApiCaller;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let caller = ApiCaller::new();

    let token = "some-bucket-token";

    // Get bucket information
    let info = caller.get_bucket(token).await?;

    // You can now get access to the file information for files inside the bucket
    for file in info.files.iter() {
        // Do something with the file information
    }

    Ok(())
}

§Create an Album

use waifuvault::ApiCaller;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let caller = ApiCaller::new();

    let token = "some-bucket-token";

    // Create a new album
    let album_info = caller.create_album(token, "new_album").await?;

    // You now have access to the album token to perform actions on the album

    Ok(())
}

§Associate Files With An Album

use waifuvault::ApiCaller;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let caller = ApiCaller::new();

    let album_tkn = "album-tkn";
    let file_1_tkn = "file_1_tkn";
    let file_2_tkn = "file_2_tkn";

    // Associate both files with the album
    let album_info = caller.associate_with_album(album_tkn, &[file_1_tkn, file_2_tkn]).await?;

    // Both files should now be part of the album

    Ok(())
}

§Disassociate Files From An Album

use waifuvault::ApiCaller;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let caller = ApiCaller::new();

    let album_tkn = "album-tkn";
    let file_1_tkn = "file_1_tkn";
    let file_2_tkn = "file_2_tkn";

    // Associate both files with the album
    let album_info = caller.disassociate_from_album(album_tkn, &[file_1_tkn, file_2_tkn]).await?;

    // Both files should now be removed from the album

    Ok(())
}

§Delete An Album

use waifuvault::ApiCaller;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let caller = ApiCaller::new();

    let album_tkn = "album-tkn";

    // Delete an album but keep the files in the bucket
    let status = caller.delete_album(album_tkn, false).await?;
     
    // We can also delete the album and any files from the bucket as well
    let status = caller.delete_album(album_tkn, true).await?;

    Ok(())
}

§Get an Album

use waifuvault::ApiCaller;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let caller = ApiCaller::new();

    let album_tkn = "album-tkn";

    // Get information about the contents of an album
    let album_info = caller.get_album(album_tkn).await?;

    Ok(())
}

§Share an Album

use waifuvault::ApiCaller;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let caller = ApiCaller::new();

    let album_tkn = "album-tkn";

    // Obtain a public URL to the album
    let status = caller.share_album(album_tkn).await?;

    // The description contains the public URL you can use to access the album
    // on the web
    let public_url = status.description;

    Ok(())
}

§Revoke Access to a Public Album

use waifuvault::ApiCaller;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let caller = ApiCaller::new();

    let album_tkn = "album-tkn";

    // Revoke access to the public album
    // This will invalidate the Public URL to the album making it inaccessible
    let status = caller.revoke_album(album_tkn).await?;

    Ok(())
}

§Download a Zip Archive of an Album

use waifuvault::ApiCaller;
use std::io::Write;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let caller = ApiCaller::new();

    let album_tkn = "album-tkn";

    // If the `file_ids` passed is `None`, it will download the entire album
    let contents = caller.download_album(album_tkn, None).await?;

    // If you know the File IDs you want to download, you can specify them
    // This will only download those files from the album
    let contents = caller.download_album(album_tkn, Some(&[0, 1, 2])).await?;

    // You can then unzip them in code or save them to disk like so
    let mut f = std::fs::File::create("archive.zip")?;
    f.write_all(&contents)?;

    Ok(())
}

Modules§

api
API types that can be received from the Waifu Vault API

Structs§

ApiCaller
Api controller which calls the endpoint