Skip to content

Instantly share code, notes, and snippets.

@tanaka-takayoshi
Last active December 15, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaka-takayoshi/5272456 to your computer and use it in GitHub Desktop.
Save tanaka-takayoshi/5272456 to your computer and use it in GitHub Desktop.
protected async override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
var cli = await Client.ConnectClient();
var user = await cli.GetMe();
var quota = await cli.GetStorageQuota();
userText.Text = user.Name != null ? string.Format("ようこそ{0}さん", user.Name) : "ようこそ";
quotaText.Text = string.Format("{0}%使用中({1} bytes中{2} 使用可能)",
((int)(quota.AvailableBytes / quota.QuotaBytes) * 100),
quota.QuotaBytes, quota.AvailableBytes);
}
using Microsoft.Live;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage;
namespace SkyDriveClient
{
public class Client
{
public static async Task<Client> ConnectClient()
{
var authClient = new LiveAuthClient();
// スコープを指定してサインインの要求
var authResult = await authClient.LoginAsync(
new[] { "wl.skydrive", "wl.contacts_photos", "wl.skydrive_update" });
if (authResult.Status != LiveConnectSessionStatus.Connected)
{
throw new Exception("SkyDrive の接続に失敗しました。");
}
return new Client(authClient.Session);
}
private LiveConnectSession session;
private Client(LiveConnectSession session)
{
this.session = session;
}
public async Task<IEnumerable<Item>> ListAlbums()
{
var liveClient = new LiveConnectClient(session);
var items = (await liveClient.GetAsync("/me/albums")).Result["data"]
as IEnumerable<dynamic>;
return items.Select(item => new Item()
{
Description = item.description,
Id = item.id,
Name = item.name,
Type = item.type
});
}
public async Task<IEnumerable<VideoItem>> ListVideoFiles(string folderId)
{
var liveClient = new LiveConnectClient(session);
var items = (await liveClient.GetAsync(folderId + "/files")).Result["data"]
as IEnumerable<dynamic>;
return items.Where(item => item.type= "video")
.Select(item => new VideoItem()
{
Description = item.description,
Id = item.id,
Name = item.name,
PicturePath = item.picture,
VideoPath = item.source
});
}
public async Task BackgroundUploadAsync(string folder, string name, IStorageFile file, OverwriteOption option,
CancellationToken token, IProgress<LiveOperationProgress> progress)
{
var liveClient = new LiveConnectClient(session);
await liveClient.BackgroundUploadAsync(folder, name, file, option, token, progress);
}
public async Task<DiskQuota> GetStorageQuota()
{
var liveClient = new LiveConnectClient(session);
dynamic result = (await liveClient.GetAsync("/me/skydrive/quota")).Result;
return new DiskQuota()
{
AvailableBytes = (ulong)result.available,
QuotaBytes = (ulong)result.quota
};
}
/// <summary>
/// dynamicを使う書き方
/// </summary>
/// <returns></returns>
public async Task<Me> GetMe()
{
var liveClient = new LiveConnectClient(session);
dynamic result = (await liveClient.GetAsync("/me")).Result;
return new Me()
{
Id = result.id,
FirstName = result.first_name,
LastName = result.last_name,
Name = result.name,
Locale = result.locale
};
}
/// <summary>
/// dynamic を使わない書き方
/// </summary>
/// <returns></returns>
public async Task<Me> GetMe2()
{
var liveClient = new LiveConnectClient(session);
var result = (await liveClient.GetAsync("/me")).Result;
return new Me()
{
Id = (string)result["id"],
FirstName = (string)result["first_name"],
LastName = (string)result["last_name"],
Name = (string)result["name"],
Locale = (string)result["locale"]
};
}
}
public class Me
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Name { get; set; }
public string Locale { get; set; }
}
public class Item
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Type { get; set; }
}
public class VideoItem : Item
{
public VideoItem()
{
Type = "video";
}
public string PicturePath { get; set; }
public string VideoPath { get; set; }
}
public class DiskQuota
{
public ulong QuotaBytes { get; set; }
public ulong AvailableBytes { get; set; }
}
}
public static async void LoadRemoteDataAsync()
{
var cli = await Client.ConnectClient();
var albums = await cli.ListAlbums();
_skydriveDataSource.AllGroups.Clear();
var albumQuery =
albums.Select(album =>
new SkyDriveFolder(album.Id, album.Name, album.Description, @"ms-appx:///Assets/IT002.png", ""));
foreach (var album in albumQuery)
{
var fileQuery =
(await cli.ListVideoFiles(album.UniqueId))
.Select(video => new SkyDriveItem(
video.Id,
video.Name,
video.Name,
video.PicturePath,
video.Description,
video.Description,
album)
{
VideoSource = video.VideoPath
});
foreach(var file in fileQuery)
{
album.Items.Add(file);
}
_skydriveDataSource.AllGroups.Add(album);
}
}
try
{
if (savedFile != null)
{
var cli = await Client.ConnectClient();
var quota = await cli.GetStorageQuota();
var prop = await savedFile.GetBasicPropertiesAsync();
if (quota.AvailableBytes < prop.Size)
{
infoTextBlock.Text = "SkyDriveの容量が足りません。ファイルの容量は " + prop.Size + " Bytesです。";
return;
}
this.progressBar.Value = 0;
var progressHandler = new Progress<LiveOperationProgress>(
(progress) => { this.progressBar.Value = progress.ProgressPercentage; });
ctsUpload = new System.Threading.CancellationTokenSource();
await cli.BackgroundUploadAsync("folder.b266a6b27ec808ac.B266A6B27EC808AC!9027",
fileName, savedFile, OverwriteOption.Overwrite, ctsUpload.Token, progressHandler);
ctsUpload = null;
this.infoTextBlock.Text = "Upload completed.";
}
}
catch (System.Threading.Tasks.TaskCanceledException)
{
this.infoTextBlock.Text = "アップロードをキャンセルしました";
}
catch (LiveConnectException)
{
this.infoTextBlock.Text = "アップロード中にエラーが
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment