-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
208 additions
and
185 deletions.
There are no files selected for viewing
11 changes: 7 additions & 4 deletions
11
MediaGallery/MediaFile/IMediaFile.cs → MediaGallery/MediaFile/IMediaFile.shared.cs
This file contains 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 |
---|---|---|
@@ -1,16 +1,19 @@ | ||
using System.IO; | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace Xamarin.MediaGallery | ||
{ | ||
public interface IMediaFile | ||
public interface IMediaFile : IDisposable | ||
{ | ||
string FileName { get; } | ||
string FileNameWithoutExtension { get; } | ||
|
||
string Extension { get; } | ||
|
||
string ContentType { get; } | ||
|
||
MediaFileType? Type { get; } | ||
|
||
Task<Stream> OpenReadAsync(); | ||
} | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,20 +1,75 @@ | ||
using System; | ||
using System.IO; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Foundation; | ||
using MobileCoreServices; | ||
using UIKit; | ||
|
||
namespace Xamarin.MediaGallery | ||
{ | ||
public partial class MediaFile | ||
internal partial class MediaFile | ||
{ | ||
internal static MediaFile Create(string fileName, Func<Task<Stream>> openReadAsync, string typeId, string extension = null) | ||
protected virtual Task<Stream> PlatformOpenReadAsync() | ||
=> Task.FromResult<Stream>(null); | ||
|
||
protected virtual void PlatformDispose() { } | ||
|
||
protected string GetExtension(string identifier) | ||
=> UTType.CopyAllTags(identifier, UTType.TagClassFilenameExtension)?.FirstOrDefault(); | ||
|
||
protected string GetMIMEType(string identifier) | ||
=> UTType.CopyAllTags(identifier, UTType.TagClassMIMEType)?.FirstOrDefault(); | ||
} | ||
|
||
internal class PHPickerFile : MediaFile | ||
{ | ||
readonly string identifier; | ||
NSItemProvider provider; | ||
|
||
internal PHPickerFile(NSItemProvider provider) | ||
{ | ||
this.provider = provider; | ||
FileNameWithoutExtension = provider?.SuggestedName; | ||
identifier = provider?.RegisteredTypeIdentifiers?.FirstOrDefault(); | ||
|
||
if (string.IsNullOrWhiteSpace(identifier)) | ||
return; | ||
|
||
Extension = GetExtension(identifier); | ||
ContentType = GetMIMEType(identifier); | ||
} | ||
|
||
protected override async Task<Stream> PlatformOpenReadAsync() | ||
=> (await provider?.LoadDataRepresentationAsync(identifier))?.AsStream(); | ||
|
||
protected override void PlatformDispose() | ||
{ | ||
typeId ??= UTType.CreatePreferredIdentifier(UTType.TagClassFilenameExtension, extension, null); | ||
extension ??= UTType.CopyAllTags(typeId, UTType.TagClassFilenameExtension)?.FirstOrDefault(); | ||
var mimeType = UTType.CopyAllTags(typeId, UTType.TagClassMIMEType).FirstOrDefault(); | ||
provider?.Dispose(); | ||
provider = null; | ||
base.PlatformDispose(); | ||
} | ||
} | ||
|
||
internal class UIDocumentFile : MediaFile | ||
{ | ||
UIDocument document; | ||
|
||
return new MediaFile(fileName, extension, mimeType, openReadAsync); | ||
internal UIDocumentFile(NSUrl assetUrl) | ||
{ | ||
document = new UIDocument(assetUrl); | ||
Extension = document.FileUrl.PathExtension; | ||
ContentType = GetMIMEType(document.FileType); | ||
FileNameWithoutExtension = document.LocalizedName; | ||
} | ||
|
||
protected override Task<Stream> PlatformOpenReadAsync() | ||
=> Task.FromResult<Stream>(File.OpenRead(document.FileUrl.Path)); | ||
|
||
protected override void PlatformDispose() | ||
{ | ||
document?.Dispose(); | ||
document = null; | ||
base.PlatformDispose(); | ||
} | ||
} | ||
} |
This file contains 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,14 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace Xamarin.MediaGallery | ||
{ | ||
internal partial class MediaFile : IMediaFile | ||
{ | ||
Task<Stream> PlatformOpenReadAsync() | ||
=> throw MediaGallery.NotSupportedOrImplementedException; | ||
|
||
void PlatformDispose() | ||
=> throw MediaGallery.NotSupportedOrImplementedException; | ||
} | ||
} |
This file contains 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 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 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 |
---|---|---|
@@ -1,10 +1,16 @@ | ||
using UIKit; | ||
using System; | ||
using UIKit; | ||
using Xamarin.Essentials; | ||
|
||
namespace Xamarin.MediaGallery | ||
{ | ||
public static partial class MediaGallery | ||
{ | ||
internal static bool HasOSVersion(int major) => | ||
UIDevice.CurrentDevice.CheckSystemVersion(major, 0); | ||
|
||
static UIViewController GetCurrentUIViewController() | ||
=> Platform.GetCurrentUIViewController() | ||
?? throw new InvalidOperationException("Could not find current view controller."); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.