mirror of
https://github.com/NotAShelf/stash.git
synced 2026-04-13 06:23:47 +00:00
multicall: prevent newline corruption of binary data in wl-copy
Previously we unconditionally appended a newline to all clipboard contents, which ended up corrupting binary files like PNG images when using shell redirection (e.g., `wl-paste > file.png`). Now we intelligently (in quotes) detect content type via MIME type and only append newlines to text-based content such as `text/*`, `application/json` and so on. Binary data on another hand is written exactly as it is. Falls back to UTF-8 validation when MIME type is unavailable. On paper this is also fully backwards compatible; text content still gets newline by default *unless* the `--no-newline` flag is used. Fixes #52 Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I8b1e6f7013d081150be761820cafd1926a6a6964
This commit is contained in:
parent
1f0312b2f6
commit
bbfe583423
1 changed files with 15 additions and 2 deletions
|
|
@ -366,7 +366,7 @@ fn handle_regular_paste(
|
||||||
let mime_type = get_paste_mime_type(args.mime_type.as_deref());
|
let mime_type = get_paste_mime_type(args.mime_type.as_deref());
|
||||||
|
|
||||||
match get_contents(clipboard, seat, mime_type) {
|
match get_contents(clipboard, seat, mime_type) {
|
||||||
Ok((mut reader, _types)) => {
|
Ok((mut reader, types)) => {
|
||||||
let mut out = io::stdout();
|
let mut out = io::stdout();
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
let mut temp_buffer = [0; 8192];
|
let mut temp_buffer = [0; 8192];
|
||||||
|
|
@ -396,7 +396,20 @@ fn handle_regular_paste(
|
||||||
if let Err(e) = out.write_all(&buf) {
|
if let Err(e) = out.write_all(&buf) {
|
||||||
bail!("failed to write to stdout: {e}");
|
bail!("failed to write to stdout: {e}");
|
||||||
}
|
}
|
||||||
if !args.no_newline && !buf.ends_with(b"\n") {
|
|
||||||
|
// Only add newline for text content, not binary data
|
||||||
|
// Check if the MIME type indicates text content
|
||||||
|
let is_text_content = if !types.is_empty() {
|
||||||
|
types.starts_with("text/")
|
||||||
|
|| types == "application/json"
|
||||||
|
|| types == "application/xml"
|
||||||
|
|| types == "application/x-sh"
|
||||||
|
} else {
|
||||||
|
// If no MIME type, check if content is valid UTF-8
|
||||||
|
std::str::from_utf8(&buf).is_ok()
|
||||||
|
};
|
||||||
|
|
||||||
|
if !args.no_newline && is_text_content && !buf.ends_with(b"\n") {
|
||||||
if let Err(e) = out.write_all(b"\n") {
|
if let Err(e) = out.write_all(b"\n") {
|
||||||
bail!("failed to write newline to stdout: {e}");
|
bail!("failed to write newline to stdout: {e}");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue