Skip to content

Commit

Permalink
Merge pull request #2035 from GraniteStateHacker/GraniteStateHacker
Browse files Browse the repository at this point in the history
Graph authentication improvements
  • Loading branch information
azchohfi authored May 23, 2018
2 parents 0f80544 + 33e5e0c commit 4381c30
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 126 deletions.
4 changes: 2 additions & 2 deletions Microsoft.Toolkit.Services/Microsoft.Toolkit.Services.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Graph" Version="1.6.2" />
<PackageReference Include="Microsoft.Identity.Client" Version="1.1.0-preview" />
<PackageReference Include="Microsoft.Graph" Version="1.8.1" />
<PackageReference Include="Microsoft.Identity.Client" Version="1.1.2-preview0008" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="System.Net.Http" Version="4.3.3" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
// ******************************************************************

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Graph;

namespace Microsoft.Toolkit.Services.OneDrive
{
internal delegate void InternalEventHandler();

/// <summary>
/// Class representing a OneDrive file
/// </summary>
public class OneDriveStorageFile : OneDriveStorageItem
{
private event InternalEventHandler ThumbnailRequestedEvent;

/// <summary>
/// Gets or sets platform-specific implementation of platform services.
/// </summary>
Expand All @@ -39,6 +44,11 @@ public string FileType
}
}

/// <summary>
/// Gets the smallest available thumbnail for the object. This will be null until you call GetThumbnailAsync().
/// </summary>
public string Thumbnail { get; private set; }

/// <summary>
/// Parse the extension of the file from its name
/// </summary>
Expand Down Expand Up @@ -83,5 +93,28 @@ public OneDriveStorageFile(IBaseClient graphProvider, IBaseRequestBuilder reques
var renameItem = await base.RenameAsync(desiredName, cancellationToken);
return InitializeOneDriveStorageFile(renameItem.OneDriveItem);
}

/// <summary>
/// Acquires the smallest available thumbnail url as string for the OneDrive file item, asyncrounously, and applies it to the Thumbnail property.
/// </summary>
/// <returns>awaitable task</returns>
public async Task UpdateThumbnailPropertyAsync()
{
var newValue = Thumbnail;

try
{
var set = await GetThumbnailSetAsync();
if (set != null)
{
newValue = set.Small ?? set.Medium ?? set.Large;
}
}
catch (Exception)
{
}

Thumbnail = newValue;
}
}
}
177 changes: 70 additions & 107 deletions Microsoft.Toolkit.Services/Services/OneDrive/OneDriveStorageItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,131 +26,104 @@ namespace Microsoft.Toolkit.Services.OneDrive
public class OneDriveStorageItem
{
/// <summary>
/// Gets or sets platform-specific implementation of platform services.
/// Gets platform-specific implementation of platform services.
/// </summary>
public IOneDriveStorageItemPlatform StorageItemPlatformService { get; set; }

private DateTimeOffset? _dateCreated;
public IOneDriveStorageItemPlatform StorageItemPlatformService { get; private set; }

/// <summary>
/// Gets the date and time that the current OneDrive item was created.
/// </summary>
public DateTimeOffset? DateCreated
{
get
{
return _dateCreated;
}
}

private DateTimeOffset? _dateModified;
public DateTimeOffset? DateCreated { get; private set; }

/// <summary>
/// Gets the date and time that the current OneDrive item was last modified.
/// </summary>
public DateTimeOffset? DateModified
{
get { return _dateModified; }
}

private string _displayName;
public DateTimeOffset? DateModified { get; private set; }

/// <summary>
/// Gets the user-friendly name of the current folder.
/// </summary>
public string DisplayName
{
get
{
return _displayName;
}
}

private string _displayType;
public string DisplayName { get; private set; }

/// <summary>
/// Gets The user-friendly type of the item.
/// </summary>
public string DisplayType
{
get
{
return _displayType;
}
}

private string _folderId;
public string DisplayType { get; private set; }

/// <summary>
/// Gets the id of the current OneDrive Item.
/// </summary>
public string FolderRelativeId
{
get
{
return _folderId;
}
}

private string _name;
public string FolderRelativeId { get; private set; }

/// <summary>
/// Gets the name of the current OneDrive Item.
/// </summary>
public string Name
{
get
{
return _name;
}
}

private string _path;
public string Name { get; private set; }

/// <summary>
/// Gets the path of the current item if the path is available
/// </summary>
public string Path
{
get
{
return _path;
}
}
public string Path { get; private set; }

/// <summary>
/// Store a reference to an instance of the underlying data provider.
/// Gets the size of the current item if available
/// </summary>
private IBaseClient _oneDriveProvider;
public long? FileSize { get; private set; }

/// <summary>
/// Store a reference to an instance of current request builder
/// Gets the file size with reasonable formatting
/// </summary>
private IBaseRequestBuilder _requestBuilder;
public string FormattedFileSize
{
get
{
var size = FileSize.HasValue ? FileSize.Value : 0;
if (size < 1024)
{
return size.ToString("F0") + " bytes";
}
else if ((size >> 10) < 1024)
{
return (size / (float)1024).ToString("F1") + " KB";
}
else if ((size >> 20) < 1024)
{
return ((size >> 10) / (float)1024).ToString("F1") + " MB";
}
else if ((size >> 30) < 1024)
{
return ((size >> 20) / (float)1024).ToString("F1") + " GB";
}
else if ((size >> 40) < 1024)
{
return ((size >> 30) / (float)1024).ToString("F1") + " TB";
}
else if ((size >> 50) < 1024)
{
return ((size >> 40) / (float)1024).ToString("F1") + " PB";
}
else
{
return ((size >> 50) / (float)1024).ToString("F0") + " EB";
}
}
}

/// <summary>
/// Gets an Item Request Builder instance
/// </summary>
public IBaseRequestBuilder RequestBuilder => _requestBuilder;
public IBaseRequestBuilder RequestBuilder { get; private set; }

/// <summary>
/// Gets or sets IOneDriveServiceClient instance
/// Gets IOneDriveServiceClient instance
/// </summary>
public IBaseClient Provider
{
get { return _oneDriveProvider; }
set { _oneDriveProvider = value; }
}
public IBaseClient Provider { get; private set; }

private DriveItem _oneDriveItem;

/// <summary>
/// Gets an instance of a DriveItem
/// </summary>
public DriveItem OneDriveItem
{
get { return _oneDriveItem; }
}
public DriveItem OneDriveItem { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="OneDriveStorageItem"/> class.
Expand All @@ -162,25 +135,26 @@ public OneDriveStorageItem(IBaseClient oneDriveProvider, IBaseRequestBuilder req
{
StorageItemPlatformService = OneDriveService.ServicePlatformInitializer.CreateOneDriveStorageItemPlatformInstance(OneDriveService.Instance, this);

_requestBuilder = requestBuilder;
_oneDriveProvider = oneDriveProvider;
_oneDriveItem = oneDriveItem;
_name = oneDriveItem.Name;
_dateCreated = oneDriveItem.CreatedDateTime;
_dateModified = oneDriveItem.LastModifiedDateTime;
_displayName = _name;
_folderId = oneDriveItem.Id;
RequestBuilder = requestBuilder;
Provider = oneDriveProvider;
OneDriveItem = oneDriveItem;
Name = oneDriveItem.Name;
FileSize = oneDriveItem.Size;
DateCreated = oneDriveItem.CreatedDateTime;
DateModified = oneDriveItem.LastModifiedDateTime;
DisplayName = Name;
FolderRelativeId = oneDriveItem.Id;
if (IsFile())
{
_displayType = "File";
DisplayType = "File";
}
else if (IsFolder())
{
_displayType = "Folder";
DisplayType = "Folder";
}
else
{
_displayType = "OneNote";
DisplayType = "OneNote";
}

// ParentReference null means is root
Expand All @@ -190,7 +164,7 @@ public OneDriveStorageItem(IBaseClient oneDriveProvider, IBaseRequestBuilder req
int index = oneDriveItem.ParentReference.Path.LastIndexOf(rootMarker) + rootMarker.Length;
if (index >= 0)
{
_path = oneDriveItem.ParentReference.Path.Substring(index);
Path = oneDriveItem.ParentReference.Path.Substring(index);
}
}
}
Expand All @@ -202,7 +176,7 @@ public OneDriveStorageItem(IBaseClient oneDriveProvider, IBaseRequestBuilder req
/// <returns> No object or value is returned by this method when it completes.</returns>
public Task DeleteAsync(CancellationToken cancellationToken = default(CancellationToken))
{
if (_name == "root")
if (Name == "root")
{
throw new Microsoft.Graph.ServiceException(new Error { Message = "Could not delete the root folder" });
}
Expand Down Expand Up @@ -230,7 +204,7 @@ public OneDriveStorageItem(IBaseClient oneDriveProvider, IBaseRequestBuilder req
{
DriveItem newOneDriveItem = new DriveItem { Name = desiredName, Description = "Item Renamed from Windows Community Toolkit" };
var itemRenamed = await ((IDriveItemRequestBuilder)RequestBuilder).Request().UpdateAsync(newOneDriveItem, cancellationToken).ConfigureAwait(false);
return new OneDriveStorageItem(_oneDriveProvider, RequestBuilder, itemRenamed);
return new OneDriveStorageItem(Provider, RequestBuilder, itemRenamed);
}

/// <summary>
Expand Down Expand Up @@ -315,28 +289,19 @@ public OneDriveStorageItem(IBaseClient oneDriveProvider, IBaseRequestBuilder req
/// Check if the item is a folder
/// </summary>
/// <returns>Return true if it's a folder</returns>
public bool IsFolder()
{
return OneDriveItem.Folder != null;
}
public bool IsFolder() => OneDriveItem.Folder != null;

/// <summary>
/// Check if the item is a file
/// </summary>
/// <returns>Return true if it's a file</returns>
public bool IsFile()
{
return OneDriveItem.File != null;
}
public bool IsFile() => OneDriveItem.File != null;

/// <summary>
/// Check if the item is a OneNote focument
/// </summary>
/// <returns>Return true if it's a OneNote document</returns>
public bool IsOneNote()
{
return !IsFile() && !IsFolder();
}
public bool IsOneNote() => !IsFile() && !IsFolder();

/// <summary>
/// Initialize a GraphOneDriveStorageFolder
Expand Down Expand Up @@ -372,8 +337,6 @@ internal OneDriveStorageFile InitializeOneDriveStorageFile(DriveItem oneDriveIte
}

internal IDriveRequestBuilder GetDriveRequestBuilderFromDriveId(string driveId)
{
return (IBaseRequestBuilder)((IGraphServiceClient)Provider).Drives[driveId] as IDriveRequestBuilder;
}
=> (IBaseRequestBuilder)((IGraphServiceClient)Provider).Drives[driveId] as IDriveRequestBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
<Version>1.0.2</Version>
</PackageReference>
<PackageReference Include="Microsoft.Graph">
<Version>1.6.2</Version>
<Version>1.8.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.Xaml.Behaviors.Uwp.Managed">
<Version>1.1.0</Version>
Expand Down Expand Up @@ -334,7 +334,7 @@
<Content Include="SamplePages\MasterDetailsView\MasterDetailsView.png" />
<Content Include="SamplePages\MasterDetailsView\OneDriveLogo.png" />
<Content Include="SamplePages\Menu\Menu.png" />
<Content Include="SamplePages\Microsoft Graph Service\OfficeLogo.png" />
<Content Include="SamplePages\Microsoft Graph Service\mslogo.png" />
<Content Include="SamplePages\Microsoft Graph Service\user.png" />
<Content Include="SamplePages\Microsoft Translator Service\TranslatorService.png" />
<Content Include="SamplePages\NavigationViewStyles\NavigationViewStyles.png" />
Expand Down
Loading

0 comments on commit 4381c30

Please sign in to comment.