Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 41 additions & 67 deletions src/Essentials/src/MediaPicker/MediaPicker.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,14 @@ public async Task<FileResult> CaptureAsync(MediaPickerOptions options, bool phot
using var inputStream = File.OpenRead(captureResult);
var fileName = System.IO.Path.GetFileName(captureResult);
using var rotatedStream = await ImageProcessor.RotateImageAsync(inputStream, fileName);

var rotatedPath = System.IO.Path.Combine(
System.IO.Path.GetDirectoryName(captureResult),
System.IO.Path.GetFileNameWithoutExtension(captureResult) + "_rotated" + System.IO.Path.GetExtension(captureResult));

using var outputStream = File.Create(rotatedPath);
rotatedStream.Position = 0;
await rotatedStream.CopyToAsync(outputStream);

// Use the rotated image and delete the original
try
{
File.Delete(captureResult);
}
catch { }
captureResult = rotatedPath;
rotatedStream.Position = 0;

// Delete original file
try { File.Delete(captureResult); } catch { }

// Write rotated image to original path (preserves filename)
using var outputStream = File.Create(captureResult);
await rotatedStream.CopyToAsync(outputStream);
}

// Apply compression/resizing if needed for photos
Expand Down Expand Up @@ -165,17 +157,14 @@ void OnResult(Intent intent)
using var inputStream = File.OpenRead(path);
var fileName = System.IO.Path.GetFileName(path);
using var rotatedStream = await ImageProcessor.RotateImageAsync(inputStream, fileName);

var rotatedPath = System.IO.Path.Combine(
System.IO.Path.GetDirectoryName(path),
System.IO.Path.GetFileNameWithoutExtension(path) + "_rotated" + System.IO.Path.GetExtension(path));

using var outputStream = File.Create(rotatedPath);
rotatedStream.Position = 0;
await rotatedStream.CopyToAsync(outputStream);

// Use the rotated image
path = rotatedPath;
// Delete original file
try { File.Delete(path); } catch { }

// Write rotated image to original path (preserves filename)
using var outputStream = File.Create(path);
await rotatedStream.CopyToAsync(outputStream);

Copilot AI Jan 16, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleting the original file before ensuring the processed stream is successfully written creates a risk of data loss. If the file creation or copy operation fails after deletion, the original file cannot be recovered. Consider writing to a temporary file first, then deleting the original and renaming the temporary file, or ensure the write operation completes successfully before deleting the original.

Copilot uses AI. Check for mistakes.
}

// Apply compression/resizing if needed
Expand Down Expand Up @@ -219,17 +208,14 @@ async Task<FileResult> PickUsingPhotoPicker(MediaPickerOptions options, bool pho
using var inputStream = File.OpenRead(path);
var fileName = System.IO.Path.GetFileName(path);
using var rotatedStream = await ImageProcessor.RotateImageAsync(inputStream, fileName);

var rotatedPath = System.IO.Path.Combine(
System.IO.Path.GetDirectoryName(path),
System.IO.Path.GetFileNameWithoutExtension(path) + "_rotated" + System.IO.Path.GetExtension(path));

using var outputStream = File.Create(rotatedPath);
rotatedStream.Position = 0;
await rotatedStream.CopyToAsync(outputStream);

// Use the rotated image
path = rotatedPath;
// Delete original file
try { File.Delete(path); } catch { }

// Write rotated image to original path (preserves filename)
using var outputStream = File.Create(path);
await rotatedStream.CopyToAsync(outputStream);

Copilot AI Jan 16, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleting the original file before ensuring the processed stream is successfully written creates a risk of data loss. If the file creation or copy operation fails after deletion, the original file cannot be recovered. Consider writing to a temporary file first, then deleting the original and renaming the temporary file, or ensure the write operation completes successfully before deleting the original.

Copilot uses AI. Check for mistakes.
}

// Apply compression/resizing if needed
Expand Down Expand Up @@ -290,17 +276,14 @@ async Task<List<FileResult>> PickMultipleUsingPhotoPicker(MediaPickerOptions opt
using var inputStream = File.OpenRead(path);
var fileName = System.IO.Path.GetFileName(path);
using var rotatedStream = await ImageProcessor.RotateImageAsync(inputStream, fileName);

var rotatedPath = System.IO.Path.Combine(
System.IO.Path.GetDirectoryName(path),
System.IO.Path.GetFileNameWithoutExtension(path) + "_rotated" + System.IO.Path.GetExtension(path));

using var outputStream = File.Create(rotatedPath);
rotatedStream.Position = 0;
await rotatedStream.CopyToAsync(outputStream);

// Use the rotated image
path = rotatedPath;
// Delete original file
try { File.Delete(path); } catch { }

// Write rotated image to original path (preserves filename)
using var outputStream = File.Create(path);
await rotatedStream.CopyToAsync(outputStream);

Copilot AI Jan 16, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleting the original file before ensuring the processed stream is successfully written creates a risk of data loss. If the file creation or copy operation fails after deletion, the original file cannot be recovered. Consider writing to a temporary file first, then deleting the original and renaming the temporary file, or ensure the write operation completes successfully before deleting the original.

Copilot uses AI. Check for mistakes.
}

// Apply compression/resizing if needed
Expand Down Expand Up @@ -368,21 +351,15 @@ static async Task<string> CompressImageIfNeeded(string imagePath, MediaPickerOpt

if (processedStream != null)
{
// Determine output extension based on processed data and original filename
var outputExtension = ImageProcessor.DetermineOutputExtension(processedStream, options?.CompressionQuality ?? 100, inputFileName);
var processedFileName = System.IO.Path.GetFileNameWithoutExtension(imagePath) + "_processed" + outputExtension;
var processedPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(imagePath), processedFileName);

// Write processed image to file
using var outputStream = File.Create(processedPath);
processedStream.Position = 0;
await processedStream.CopyToAsync(outputStream);

// Delete original file
try
{ originalFile.Delete(); }
catch { }
return processedPath;
// Delete original file first
try { originalFile.Delete(); } catch { }

// Write processed image to original path (preserves filename)
using var outputStream = File.Create(imagePath);
processedStream.Position = 0;
await processedStream.CopyToAsync(outputStream);

return imagePath;

Copilot AI Jan 16, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleting the original file before ensuring the processed stream is successfully written creates a risk of data loss. If the file creation or copy operation fails after deletion, the original file cannot be recovered. Consider writing to a temporary file first, then deleting the original and renaming the temporary file, or ensure the write operation completes successfully before deleting the original.

Suggested change
// Delete original file first
try { originalFile.Delete(); } catch { }
// Write processed image to original path (preserves filename)
using var outputStream = File.Create(imagePath);
processedStream.Position = 0;
await processedStream.CopyToAsync(outputStream);
return imagePath;
// Write processed image to a temporary file first to avoid data loss
var directory = System.IO.Path.GetDirectoryName(imagePath);
var tempFilePath = System.IO.Path.Combine(directory ?? System.IO.Path.GetTempPath(), System.IO.Path.GetRandomFileName());
try
{
// Write processed image to temporary path
using (var outputStream = File.Create(tempFilePath))
{
processedStream.Position = 0;
await processedStream.CopyToAsync(outputStream);
}
// Now that the processed image is safely written, replace the original file
try
{
if (originalFile.Exists())
originalFile.Delete();
}
catch
{
// Ignore deletion errors; we will still attempt to move the temp file
}
// Move the temporary file to the original path (preserves filename)
File.Move(tempFilePath, imagePath, true);
return imagePath;
}
finally
{
// Best-effort cleanup of the temporary file if it still exists
try
{
if (File.Exists(tempFilePath))
File.Delete(tempFilePath);
}
catch
{
// Ignore cleanup errors
}
}

Copilot uses AI. Check for mistakes.
}

// If ImageProcessor returns null (e.g., on .NET Standard), ImageProcessor.IsProcessingNeeded would have returned false,
Expand Down Expand Up @@ -488,17 +465,14 @@ void OnResult(Intent resultIntent)
using var inputStream = File.OpenRead(processedPath);
var fileName = System.IO.Path.GetFileName(processedPath);
using var rotatedStream = await ImageProcessor.RotateImageAsync(inputStream, fileName);

var rotatedPath = System.IO.Path.Combine(
System.IO.Path.GetDirectoryName(processedPath),
System.IO.Path.GetFileNameWithoutExtension(processedPath) + "_rotated" + System.IO.Path.GetExtension(processedPath));

using var outputStream = File.Create(rotatedPath);
rotatedStream.Position = 0;
await rotatedStream.CopyToAsync(outputStream);

// Use the rotated image
processedPath = rotatedPath;
// Delete original file
try { File.Delete(processedPath); } catch { }

// Write rotated image to original path (preserves filename)
using var outputStream = File.Create(processedPath);
await rotatedStream.CopyToAsync(outputStream);
}

// Apply compression/resizing if needed
Expand Down
Loading