Use upload sessions to get rid of file limit

This commit is contained in:
SupSuper 2018-04-14 06:32:48 +01:00
parent 080bce2126
commit 61b7dcd7cf
3 changed files with 57 additions and 13 deletions

View file

@ -290,7 +290,7 @@ protected UploadResult SendRequestFile(string url, Stream data, string fileName,
return result;
}
protected UploadResult SendRequestFileRaw(string url, Stream data, string fileName, Dictionary<string, string> args = null,
protected UploadResult SendRequestBytes(string url, Stream data, string fileName, Dictionary<string, string> args = null,
NameValueCollection headers = null, CookieCollection cookies = null, ResponseType responseType = ResponseType.Text, HttpMethod method = HttpMethod.PUT)
{
UploadResult result = new UploadResult();
@ -305,6 +305,14 @@ protected UploadResult SendRequestFileRaw(string url, Stream data, string fileNa
long contentLength = data.Length;
string contentType = Helpers.GetMimeType(fileName);
if (headers == null)
{
headers = new NameValueCollection();
}
long startByte = 0;
long endByte = data.Length - 1;
headers.Add("Content-Range", $"bytes {startByte}-{endByte}/{contentLength}");
HttpWebRequest request = PrepareWebRequest(method, url, headers, cookies, contentType, contentLength);
using (Stream requestStream = request.GetRequestStream())

View file

@ -67,6 +67,8 @@ public sealed class OneDrive : FileUploader, IOAuth2
public string FolderID { get; set; }
public bool AutoCreateShareableLink { get; set; }
private string sessionUrl;
public static OneDriveFileInfo RootFolder = new OneDriveFileInfo
{
id = "", // empty defaults to root
@ -196,15 +198,43 @@ private string GetFolderUrl(string folderID)
return folderPath;
}
private string CreateSession(string fileName)
{
string json = JsonConvert.SerializeObject(new
{
item = new Dictionary<string, string>
{
{ "name", fileName },
{ "@microsoft.graph.conflictBehavior", "replace" }
}
});
string folderPath = GetFolderUrl(FolderID);
string url = URLHelpers.BuildUri("https://graph.microsoft.com", $"/v1.0/{folderPath}:/{fileName}:/createUploadSession");
string response = SendRequest(HttpMethod.POST, url, json, ContentTypeJSON, headers: GetAuthHeaders());
OneDriveUploadSession session = JsonConvert.DeserializeObject<OneDriveUploadSession>(response);
if (session != null)
{
return session.uploadUrl;
}
return null;
}
public override UploadResult Upload(Stream stream, string fileName)
{
if (!CheckAuthorization()) return null;
string folderPath = GetFolderUrl(FolderID);
if (string.IsNullOrEmpty(sessionUrl))
{
sessionUrl = CreateSession(fileName);
}
string url = URLHelpers.BuildUri("https://graph.microsoft.com", $"/v1.0/{folderPath}:/{fileName}:/content", "select=id,webUrl");
UploadResult result = SendRequestFileRaw(url, stream, fileName, headers: GetAuthHeaders());
UploadResult result = SendRequestBytes(sessionUrl, stream, fileName);
if (result.IsSuccess)
{
@ -249,7 +279,7 @@ public string CreateShareableLink(string id, OneDriveLinkType linkType = OneDriv
string response = SendRequest(HttpMethod.POST, $"https://graph.microsoft.com/v1.0/me/drive/items/{id}/createLink", json, ContentTypeJSON,
headers: GetAuthHeaders());
OneDrivePermissionInfo permissionInfo = JsonConvert.DeserializeObject<OneDrivePermissionInfo>(response);
OneDrivePermission permissionInfo = JsonConvert.DeserializeObject<OneDrivePermission>(response);
if (permissionInfo != null && permissionInfo.link != null)
{
@ -259,7 +289,7 @@ public string CreateShareableLink(string id, OneDriveLinkType linkType = OneDriv
return null;
}
public OneDrivePathInfo GetPathInfo(string id)
public OneDriveFileList GetPathInfo(string id)
{
if (!CheckAuthorization()) return null;
@ -273,7 +303,7 @@ public OneDrivePathInfo GetPathInfo(string id)
if (response != null)
{
OneDrivePathInfo pathInfo = JsonConvert.DeserializeObject<OneDrivePathInfo>(response);
OneDriveFileList pathInfo = JsonConvert.DeserializeObject<OneDriveFileList>(response);
return pathInfo;
}
@ -288,22 +318,28 @@ public class OneDriveFileInfo
public string webUrl { get; set; }
}
public class OneDrivePermissionInfo
public class OneDrivePermission
{
public OneDriveShareableLinkInfo link { get; set; }
public OneDriveShareableLink link { get; set; }
}
public class OneDriveShareableLinkInfo
public class OneDriveShareableLink
{
public string webUrl { get; set; }
public string webHtml { get; set; }
}
public class OneDrivePathInfo
public class OneDriveFileList
{
public OneDriveFileInfo[] value { get; set; }
}
public class OneDriveUploadSession
{
public string uploadUrl { get; set; }
public string[] nextExpectedRanges { get; set; }
}
public enum OneDriveLinkType
{
[Description("An embedded link, which is an HTML code snippet that you can insert into a webpage to provide an interactive view of the corresponding file.")]

View file

@ -792,7 +792,7 @@ public void OneDriveListFolders(OneDriveFileInfo fileEntry, TreeNode tnParent)
{
Application.DoEvents();
OneDrive oneDrive = new OneDrive(Config.OneDriveV2OAuth2Info);
OneDrivePathInfo oneDrivePathInfo = oneDrive.GetPathInfo(fileEntry.id);
OneDriveFileList oneDrivePathInfo = oneDrive.GetPathInfo(fileEntry.id);
tnParent.Nodes.Clear();
foreach (OneDriveFileInfo folder in oneDrivePathInfo.value)
{