Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #897 from garie/master
Browse files Browse the repository at this point in the history
Add original filename to MediaFile
  • Loading branch information
jamesmontemagno authored Feb 4, 2022
2 parents 93d6f06 + e328e1e commit 816400a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 21 deletions.
13 changes: 7 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
Expand Down Expand Up @@ -186,6 +186,7 @@ ClientBin/
*.publishsettings
node_modules/
orleans.codegen.cs
.DS_Store

# RIA/Silverlight projects
Generated_Code/
Expand Down Expand Up @@ -238,8 +239,8 @@ _Pvt_Extensions

tools/
Build/
src/.droidres/.media.droidres.db
tests/.droidres/.media.droidres.db
tests/.droidres/.media.droidres.db
tests/.droidres/.media.droidres.db
*.db
src/.droidres/.media.droidres.db
tests/.droidres/.media.droidres.db
tests/.droidres/.media.droidres.db
tests/.droidres/.media.droidres.db
*.db
2 changes: 2 additions & 0 deletions src/Media.Plugin.Sample/Media.Plugin.Sample.iOS/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,7 @@
<string>We need access to the photo library</string>
<key>NSCameraUsageDescription</key>
<string>We need to be able to take photos</string>
<key>NSMicrophoneUsageDescription</key>
<string>We need to be able to take videos</string>
</dict>
</plist>
35 changes: 23 additions & 12 deletions src/Media.Plugin/Android/MediaPickerActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ void GrantUriPermissionsForIntent(Intent intent, Uri uri)

internal static Task<MediaPickedEventArgs> GetMediaFileAsync(Context context, int requestCode, string action, bool isPhoto, ref Uri path, Uri data, bool saveToAlbum)
{
Task<Tuple<string, bool>> pathFuture;
Task<Tuple<string, string, bool>> pathFuture;

string originalPath = null;

Expand All @@ -309,12 +309,13 @@ internal static Task<MediaPickedEventArgs> GetMediaFileAsync(Context context, in
{
originalPath = data.ToString();
var currentPath = path.Path;
var originalFilename = Path.GetFileName(currentPath);
pathFuture = TryMoveFileAsync(context, data, path, isPhoto, false).ContinueWith(t =>
new Tuple<string, bool>(t.Result ? currentPath : null, false));
new Tuple<string, string, bool>(t.Result ? currentPath : null, t.Result ? originalFilename : null, false));
}
else
{
pathFuture = TaskFromResult(new Tuple<string, bool>(path.Path, false));
pathFuture = TaskFromResult(new Tuple<string, string, bool>(path.Path, Path.GetFileName(path.Path), false));

}
}
Expand All @@ -325,19 +326,20 @@ internal static Task<MediaPickedEventArgs> GetMediaFileAsync(Context context, in
pathFuture = GetFileForUriAsync(context, path, isPhoto, false);
}
else
pathFuture = TaskFromResult<Tuple<string, bool>>(null);
pathFuture = TaskFromResult<Tuple<string, string, bool>>(null);

return pathFuture.ContinueWith(t =>
{

var resultPath = t?.Result?.Item1;
var originalFilename = t?.Result?.Item2;
var aPath = originalPath;
if (resultPath != null && File.Exists(resultPath))
{
var mf = new MediaFile(resultPath, () =>
{
return File.OpenRead(resultPath);
}, albumPath: aPath);
}, albumPath: aPath, originalFilename: originalFilename);
return new MediaPickedEventArgs(requestCode, false, mf);
}
else
Expand Down Expand Up @@ -574,12 +576,16 @@ public static Uri GetOutputMediaFile(Context context, string subdir, string name
return uri;
}

internal static Task<Tuple<string, bool>> GetFileForUriAsync(Context context, Uri uri, bool isPhoto, bool saveToAlbum)
internal static Task<Tuple<string, string, bool>> GetFileForUriAsync(Context context, Uri uri, bool isPhoto, bool saveToAlbum)
{
var tcs = new TaskCompletionSource<Tuple<string, bool>>();
var tcs = new TaskCompletionSource<Tuple<string, string, bool>>();

if (uri.Scheme == "file")
tcs.SetResult(new Tuple<string, bool>(new System.Uri(uri.ToString()).LocalPath, false));
{
var path = new System.Uri(uri.ToString()).LocalPath;
var originalFilename = Path.GetFileName(path);
tcs.SetResult(new Tuple<string, string, bool>(path, originalFilename, false));
}
else if (uri.Scheme == "content")
{
Task.Factory.StartNew(() =>
Expand All @@ -593,7 +599,7 @@ internal static Task<Tuple<string, bool>> GetFileForUriAsync(Context context, Ur

cursor = context.ContentResolver.Query(uri, proj, null, null, null);
if (cursor == null || !cursor.MoveToNext())
tcs.SetResult(new Tuple<string, bool>(null, false));
tcs.SetResult(new Tuple<string, string, bool>(null, null, false));
else
{
var column = cursor.GetColumnIndex(MediaStore.MediaColumns.Data);
Expand All @@ -602,7 +608,7 @@ internal static Task<Tuple<string, bool>> GetFileForUriAsync(Context context, Ur
if (column != -1)
contentPath = cursor.GetString(column);


string originalFilename = null;

// If they don't follow the "rules", try to copy the file locally
if (contentPath == null || !contentPath.StartsWith("file", StringComparison.InvariantCultureIgnoreCase))
Expand All @@ -611,6 +617,7 @@ internal static Task<Tuple<string, bool>> GetFileForUriAsync(Context context, Ur
try
{
fileName = Path.GetFileName(contentPath);
originalFilename = fileName;
}
catch(Exception ex)
{
Expand All @@ -636,8 +643,12 @@ internal static Task<Tuple<string, bool>> GetFileForUriAsync(Context context, Ur
System.Diagnostics.Debug.WriteLine("Unable to save picked file from disk " + fnfEx);
}
}
else
{
originalFilename = Path.GetFileName(contentPath);
}

tcs.SetResult(new Tuple<string, bool>(contentPath, false));
tcs.SetResult(new Tuple<string, string, bool>(contentPath, originalFilename, false));
}
}
finally
Expand All @@ -651,7 +662,7 @@ internal static Task<Tuple<string, bool>> GetFileForUriAsync(Context context, Ur
}, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
}
else
tcs.SetResult(new Tuple<string, bool>(null, false));
tcs.SetResult(new Tuple<string, string, bool>(null, null, false));

return tcs.Task;
}
Expand Down
19 changes: 18 additions & 1 deletion src/Media.Plugin/Shared/MediaFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,29 @@ public sealed class MediaFile : IDisposable
/// <param name="path"></param>
/// <param name="streamGetter"></param>
/// <param name="albumPath"></param>
public MediaFile(string path, Func<Stream> streamGetter, Func<Stream> streamGetterForExternalStorage = null, string albumPath = null)
public MediaFile(string path, Func<Stream> streamGetter, Func<Stream> streamGetterForExternalStorage = null, string albumPath = null, string originalFilename = null)
{
this.streamGetter = streamGetter;
this.streamGetterForExternalStorage = streamGetterForExternalStorage;
this.path = path;
this.albumPath = albumPath;
this.originalFilename = originalFilename;
}

/// <summary>
/// The original filename
/// </summary>
public string OriginalFilename
{
get
{
if (isDisposed)
throw new ObjectDisposedException(null);

return originalFilename;
}
}

/// <summary>
/// Path to file
/// </summary>
Expand Down Expand Up @@ -96,6 +112,7 @@ public void Dispose()
bool isDisposed;
Func<Stream> streamGetter;
Func<Stream> streamGetterForExternalStorage;
string originalFilename;
string path;
string albumPath;

Expand Down
4 changes: 3 additions & 1 deletion src/Media.Plugin/iOS/ECLImagePickerViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,12 @@ MediaFile GetPictureMediaFile(ALAsset asset, long index = 0)
var cgImage = rep.GetImage();

UIImage image = null;
string originalFilename = null;
if (cgImage == null)
{
var fetch = PHAsset.FetchAssets(new[] { asset.AssetUrl }, null);
var ph = fetch.firstObject as PHAsset;
originalFilename = PHAssetResource.GetAssetResources(ph)?.FirstOrDefault()?.OriginalFilename;
var manager = PHImageManager.DefaultManager;
var phOptions = new PHImageRequestOptions
{
Expand Down Expand Up @@ -243,7 +245,7 @@ MediaFile GetPictureMediaFile(ALAsset asset, long index = 0)
var url = asset.AssetUrl;
aPath = url?.AbsoluteString;

return new MediaFile(path, () => File.OpenRead(path), albumPath: aPath);
return new MediaFile(path, () => File.OpenRead(path), albumPath: aPath, originalFilename: originalFilename);
}

void CancelledPicker()
Expand Down
14 changes: 13 additions & 1 deletion src/Media.Plugin/iOS/MediaPickerDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using System.Diagnostics;
using System.Drawing;
using CoreImage;
using Photos;
using System.Linq;

namespace Plugin.Media
{
Expand Down Expand Up @@ -458,7 +460,17 @@ async Task<MediaFile> GetPictureMediaFile(NSDictionary info)
return File.OpenRead(path);
};

return new MediaFile(path, () => File.OpenRead(path), streamGetterForExternalStorage: () => getStreamForExternalStorage(), albumPath: aPath);
string originalFilename = null;
if (info.TryGetValue(UIImagePickerController.PHAsset, out var assetObj))
{
var asset = (PHAsset)assetObj;
if (asset != null)
{
originalFilename = PHAssetResource.GetAssetResources(asset)?.FirstOrDefault()?.OriginalFilename;
}
}

return new MediaFile(path, () => File.OpenRead(path), streamGetterForExternalStorage: () => getStreamForExternalStorage(), albumPath: aPath, originalFilename: originalFilename);
}

internal static NSDictionary SetGpsLocation(NSDictionary meta, Location location)
Expand Down

0 comments on commit 816400a

Please sign in to comment.