Compare commits

..

No commits in common. "6337642b3c41db064ce40d91c5e04f4a321530ce" and "b7d5425a734b53ec53000ce7b46bd0dc4b931293" have entirely different histories.

3 changed files with 15 additions and 127 deletions

View file

@ -1,57 +0,0 @@
use omegaupload_common::secrecy::{ExposeSecret, SecretString};
pub struct Builder {
decryption_key: SecretString,
needs_password: bool,
file_name: Option<String>,
language: Option<String>,
}
impl Builder {
pub fn new(decryption_key: SecretString) -> Self {
Self {
decryption_key,
needs_password: false,
file_name: None,
language: None,
}
}
pub fn needs_password(mut self) -> Self {
self.needs_password = true;
self
}
pub fn file_name(mut self, name: String) -> Self {
self.file_name = Some(name);
self
}
pub fn language(mut self, language: String) -> Self {
self.language = Some(language);
self
}
pub fn build(self) -> SecretString {
if !self.needs_password && self.file_name.is_none() && self.language.is_none() {
return self.decryption_key;
}
let mut args = String::new();
if self.needs_password {
args.push_str("!pw");
}
if let Some(file_name) = self.file_name {
args.push_str("!name:");
args.push_str(&file_name);
}
if let Some(language) = self.language {
args.push_str("!lang:");
args.push_str(&language);
}
SecretString::new(format!(
"key:{}{}",
self.decryption_key.expose_secret(),
args
))
}
}

View file

@ -24,7 +24,7 @@ use anyhow::{anyhow, bail, Context, Result};
use atty::Stream;
use clap::Parser;
use omegaupload_common::crypto::{open_in_place, seal_in_place};
use omegaupload_common::secrecy::{ExposeSecret, SecretString, SecretVec};
use omegaupload_common::secrecy::{ExposeSecret, SecretVec};
use omegaupload_common::{
base64, Expiration, ParsedUrl, Url, API_ENDPOINT, EXPIRATION_HEADER_NAME,
};
@ -33,10 +33,6 @@ use reqwest::header::EXPIRES;
use reqwest::StatusCode;
use rpassword::prompt_password_stderr;
use crate::fragment::Builder;
mod fragment;
#[derive(Parser)]
struct Opts {
#[clap(subcommand)]
@ -57,13 +53,6 @@ enum Action {
/// The path to the file to upload. If none is provided, then reads
/// stdin instead.
path: Option<PathBuf>,
/// Hint that the uploaded file should be syntax highlighted with a
/// specific language.
#[clap(short, long)]
language: Option<String>,
/// Don't provide a file name hint.
#[clap(short = 'F', long)]
no_file_name_hint: bool,
},
Download {
/// The paste to download.
@ -80,9 +69,7 @@ fn main() -> Result<()> {
password,
duration,
path,
language,
no_file_name_hint,
} => handle_upload(url, password, duration, path, language, no_file_name_hint),
} => handle_upload(url, password, duration, path),
Action::Download { url } => handle_download(url),
}?;
@ -94,8 +81,6 @@ fn handle_upload(
password: bool,
duration: Option<Expiration>,
path: Option<PathBuf>,
language: Option<String>,
no_file_name_hint: bool,
) -> Result<()> {
url.set_fragment(None);
@ -104,7 +89,7 @@ fn handle_upload(
}
let (data, key) = {
let mut container = if let Some(ref path) = path {
let mut container = if let Some(path) = path {
std::fs::read(path)?
} else {
let mut container = vec![];
@ -125,7 +110,7 @@ fn handle_upload(
};
let enc_key = seal_in_place(&mut container, password)?;
let key = SecretString::new(base64::encode(&enc_key.expose_secret().as_ref()));
let key = base64::encode(&enc_key.expose_secret().as_ref());
(container, key)
};
@ -145,22 +130,13 @@ fn handle_upload(
.map_err(|_| anyhow!("Failed to get base URL"))?
.extend(std::iter::once(res.text()?));
let mut fragment = Builder::new(key);
if password {
fragment = fragment.needs_password();
}
let fragment = if password {
format!("key:{}!pw", key)
} else {
key
};
if !no_file_name_hint {
if let Some(path) = path {
fragment = fragment.file_name(path.to_string_lossy().to_string());
}
}
if let Some(language) = language {
fragment = fragment.language(language);
}
url.set_fragment(Some(&fragment.build().expose_secret()));
url.set_fragment(Some(&fragment));
println!("{}", url);

View file

@ -106,44 +106,13 @@ function createStringPasteUi(data, mimeType: string, name?: string, lang?: strin
mainEle.appendChild(preEle);
bodyEle.appendChild(mainEle);
let languages = undefined;
if (!hljs.getLanguage(lang)) {
console.warn(`[js] User provided language (${lang}) is not known.`);
console.warn(`[js] User provided language (${lang}) is not known. Ignoring.`);
} else {
languages = [lang];
}
// If a language wasn't provided, see if we can use the file extension to give
// us a better hint for hljs
if (!languages) {
if (name) {
console.log("[js] Trying to infer from file name...");
const periodIndex = name.indexOf(".");
if (periodIndex === -1) {
console.warn("[js] Did not find file extension.")
} else {
let extension = name.slice(periodIndex + 1);
console.info(`[js] Found extension ${extension}.`);
if (!hljs.getLanguage(extension)) {
console.warn(`[js] Extension was not recognized by hljs. Giving up.`);
} else {
console.info("[js] Successfully inferred language from file extension.");
languages = [extension];
}
}
} else {
console.log("[js] No file name hint provided.");
}
} else {
console.info(`[js] Selecting user provided language ${languages[0]} for highlighting.`);
}
// If we still haven't set languages here, then we're leaving it up to the
if (!languages) {
console.log("[js] Deferring to hljs inference for syntax highlighting.");
} else {
hljs.configure({ languages });
console.log(`[js] Selecting user provided language ${lang} for highlighting.`);
hljs.configure({
languages: [lang],
});
}
hljs.highlightAll();