Repository navigation
feat(desktop-gpui): add a copy file action to finished exports #3100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yashranaway
wants to merge
3
commits into
CapSoftware:main
Choose a base branch
from
yashranaway:feat/copy-file-action
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+195
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| use std::{ | ||
| fs::{File, OpenOptions}, | ||
| io::{self, ErrorKind}, | ||
| path::{Path, PathBuf}, | ||
| }; | ||
|
|
||
| fn copy_path(source: &Path, number: u64) -> PathBuf { | ||
| let mut name = source.file_stem().unwrap_or_default().to_os_string(); | ||
| name.push(" copy"); | ||
| if number > 1 { | ||
| name.push(format!(" {number}")); | ||
| } | ||
| if let Some(extension) = source.extension() { | ||
| name.push("."); | ||
| name.push(extension); | ||
| } | ||
| source.with_file_name(name) | ||
| } | ||
|
|
||
| pub fn duplicate_file(source: &Path) -> Result<PathBuf, String> { | ||
| duplicate(source).map_err(|error| format!("Failed to copy file: {error}")) | ||
| } | ||
|
|
||
| fn duplicate(source: &Path) -> io::Result<PathBuf> { | ||
| let mut input = File::open(source)?; | ||
| if !input.metadata()?.is_file() { | ||
| return Err(io::Error::new( | ||
| ErrorKind::InvalidInput, | ||
| "only files can be copied", | ||
| )); | ||
| } | ||
| let mut number = 1; | ||
| loop { | ||
| let destination = copy_path(source, number); | ||
| match OpenOptions::new() | ||
| .write(true) | ||
| .create_new(true) | ||
| .open(&destination) | ||
| { | ||
| Ok(mut output) => { | ||
| if let Err(error) = io::copy(&mut input, &mut output) { | ||
| drop(output); | ||
| let _ = std::fs::remove_file(&destination); | ||
| return Err(error); | ||
| } | ||
| return Ok(destination); | ||
| } | ||
| Err(error) if error.kind() == ErrorKind::AlreadyExists => number += 1, | ||
| Err(error) => return Err(error), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn copy_names_preserve_extensions() { | ||
| for (source, expected) in [ | ||
| ("video.mp4", "video copy.mp4"), | ||
| ("video.final.mp4", "video.final copy.mp4"), | ||
| ("recording", "recording copy"), | ||
| (".hidden", ".hidden copy"), | ||
| ("日本語.png", "日本語 copy.png"), | ||
| ("video copy.mp4", "video copy copy.mp4"), | ||
| ] { | ||
| assert_eq!(copy_path(Path::new(source), 1), Path::new(expected)); | ||
| } | ||
| assert_eq!( | ||
| copy_path(Path::new("/output/video.mp4"), 2), | ||
| Path::new("/output/video copy 2.mp4") | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn existing_copies_are_never_overwritten() { | ||
| let dir = tempfile::tempdir().unwrap(); | ||
| let source = dir.path().join("video.final.mp4"); | ||
| std::fs::write(&source, b"original").unwrap(); | ||
| std::fs::write(dir.path().join("video.final copy.mp4"), b"first").unwrap(); | ||
| std::fs::create_dir(dir.path().join("video.final copy 2.mp4")).unwrap(); | ||
| let copy = duplicate_file(&source).unwrap(); | ||
| assert_eq!(copy, dir.path().join("video.final copy 3.mp4")); | ||
| assert_eq!(std::fs::read(copy).unwrap(), b"original"); | ||
| assert_eq!( | ||
| std::fs::read(dir.path().join("video.final copy.mp4")).unwrap(), | ||
| b"first" | ||
| ); | ||
| assert_eq!(std::fs::read(source).unwrap(), b"original"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn concurrent_copies_get_distinct_names() { | ||
| let dir = tempfile::tempdir().unwrap(); | ||
| let source = dir.path().join("screenshot.png"); | ||
| std::fs::write(&source, b"pixels").unwrap(); | ||
| let copies = std::thread::scope(|scope| { | ||
| let first = scope.spawn(|| duplicate_file(&source).unwrap()); | ||
| let second = scope.spawn(|| duplicate_file(&source).unwrap()); | ||
| [first.join().unwrap(), second.join().unwrap()] | ||
| }); | ||
| assert_ne!(copies[0], copies[1]); | ||
| for copy in copies { | ||
| assert_eq!(std::fs::read(copy).unwrap(), b"pixels"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn missing_files_and_project_directories_fail_without_a_copy() { | ||
| let dir = tempfile::tempdir().unwrap(); | ||
| assert!( | ||
| duplicate_file(&dir.path().join("missing.mp4")) | ||
| .unwrap_err() | ||
| .starts_with("Failed to copy file:") | ||
| ); | ||
| let project = dir.path().join("recording.cap"); | ||
| std::fs::create_dir(&project).unwrap(); | ||
| assert!(duplicate_file(&project).is_err()); | ||
| assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 1); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.