-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from yar229/develop
fixed renaming folders with many inner items
- Loading branch information
Showing
13 changed files
with
316 additions
and
95 deletions.
There are no files selected for viewing
Submodule MailRuNetCloudClient
updated
9 files
+0 −2 | MailRuCloudApi/File.cs | |
+15 −41 | MailRuCloudApi/FileSize.cs | |
+13 −42 | MailRuCloudApi/Folder.cs | |
+10 −13 | MailRuCloudApi/JsonParser.cs | |
+4 −10 | MailRuCloudApi/MailRuCloudApi.cs | |
+1 −0 | MailRuCloudApi/MailRuCloudApi.csproj | |
+1 −3 | MailRuCloudApi/Quota.cs | |
+28 −0 | MailRuCloudApi/StorageUnit.cs | |
+1 −2 | MailRuCloudApi/UploadStream.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
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
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 |
---|---|---|
@@ -0,0 +1,155 @@ | ||
using NWebDav.Server; | ||
using NWebDav.Server.Helpers; | ||
using NWebDav.Server.Http; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Xml.Linq; | ||
using NWebDav.Server.Stores; | ||
|
||
namespace YaR.WebDavMailRu.CloudStore.Mailru | ||
{ | ||
public class MoveHandler : IRequestHandler | ||
{ | ||
/// <summary> | ||
/// Handle a MOVE request. | ||
/// </summary> | ||
/// <param name="httpContext"> | ||
/// The HTTP context of the request. | ||
/// </param> | ||
/// <param name="store"> | ||
/// Store that is used to access the collections and items. | ||
/// </param> | ||
/// <returns> | ||
/// A task that represents the asynchronous MOVE operation. The task | ||
/// will always return <see langword="true"/> upon completion. | ||
/// </returns> | ||
public async Task<bool> HandleRequestAsync(IHttpContext httpContext, IStore store) | ||
{ | ||
// Obtain request and response | ||
var request = httpContext.Request; | ||
var response = httpContext.Response; | ||
|
||
// We should always move the item from a parent container | ||
var splitSourceUri = RequestHelper.SplitUri(request.Url); | ||
|
||
// Obtain source collection | ||
var sourceCollection = await store.GetCollectionAsync(splitSourceUri.CollectionUri, httpContext).ConfigureAwait(false); | ||
if (sourceCollection == null) | ||
{ | ||
// Source not found | ||
response.SetStatus(DavStatusCode.NotFound); | ||
return true; | ||
} | ||
|
||
// Obtain the destination | ||
var destinationUri = request.GetDestinationUri(); | ||
if (destinationUri == null) | ||
{ | ||
// Bad request | ||
response.SetStatus(DavStatusCode.BadRequest, "Destination header is missing."); | ||
return true; | ||
} | ||
|
||
// Make sure the source and destination are different | ||
if (request.Url.AbsoluteUri.Equals(destinationUri.AbsoluteUri, StringComparison.CurrentCultureIgnoreCase)) | ||
{ | ||
// Forbidden | ||
response.SetStatus(DavStatusCode.Forbidden, "Source and destination cannot be the same."); | ||
return true; | ||
} | ||
|
||
// We should always move the item to a parent | ||
var splitDestinationUri = RequestHelper.SplitUri(destinationUri); | ||
|
||
// Obtain destination collection | ||
var destinationCollection = await store.GetCollectionAsync(splitDestinationUri.CollectionUri, httpContext).ConfigureAwait(false); | ||
if (destinationCollection == null) | ||
{ | ||
// Source not found | ||
response.SetStatus(DavStatusCode.NotFound); | ||
return true; | ||
} | ||
|
||
// Check if the Overwrite header is set | ||
var overwrite = request.GetOverwrite(); | ||
|
||
// Keep track of all errors | ||
var errors = new UriResultCollection(); | ||
|
||
// Move collection | ||
await MoveAsync(sourceCollection, splitSourceUri.Name, destinationCollection, splitDestinationUri.Name, overwrite, httpContext, splitDestinationUri.CollectionUri, errors).ConfigureAwait(false); | ||
|
||
// Check if there are any errors | ||
if (errors.HasItems) | ||
{ | ||
// Obtain the status document | ||
var xDocument = new XDocument(errors.GetXmlMultiStatus()); | ||
|
||
// Stream the document | ||
await response.SendResponseAsync(DavStatusCode.MultiStatus, xDocument).ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
// Set the response | ||
response.SetStatus(DavStatusCode.Ok); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private async Task MoveAsync(IStoreCollection sourceCollection, string sourceName, IStoreCollection destinationCollection, string destinationName, bool overwrite, IHttpContext httpContext, Uri baseUri, UriResultCollection errors) | ||
{ | ||
// Determine the new base URI | ||
var subBaseUri = UriHelper.Combine(baseUri, destinationName); | ||
|
||
// Items should be moved directly | ||
var result = await sourceCollection.MoveItemAsync(sourceName, destinationCollection, destinationName, overwrite, httpContext); | ||
if (result.Result != DavStatusCode.Created && result.Result != DavStatusCode.NoContent) | ||
errors.AddResult(subBaseUri, result.Result); | ||
} | ||
} | ||
|
||
internal class UriResultCollection | ||
{ | ||
private struct UriResult | ||
{ | ||
private Uri Uri { get; } | ||
private DavStatusCode Result { get; } | ||
|
||
public UriResult(Uri uri, DavStatusCode result) | ||
{ | ||
Uri = uri; | ||
Result = result; | ||
} | ||
|
||
public XElement GetXmlResponse() | ||
{ | ||
var href = Uri.AbsoluteUri; | ||
var statusText = $"HTTP/1.1 {(int)Result} {Result.GetStatusDescription()}"; | ||
return new XElement(WebDavNamespaces.DavNs + "response", | ||
new XElement(WebDavNamespaces.DavNs + "href", href), | ||
new XElement(WebDavNamespaces.DavNs + "status", statusText)); | ||
} | ||
} | ||
|
||
private readonly IList<UriResult> _results = new List<UriResult>(); | ||
|
||
public bool HasItems => _results.Any(); | ||
|
||
public void AddResult(Uri uri, DavStatusCode result) | ||
{ | ||
_results.Add(new UriResult(uri, result)); | ||
} | ||
|
||
public XElement GetXmlMultiStatus() | ||
{ | ||
var xMultiStatus = new XElement(WebDavNamespaces.DavNs + "multistatus"); | ||
foreach (var result in _results) | ||
xMultiStatus.Add(result.GetXmlResponse()); | ||
return xMultiStatus; | ||
} | ||
} | ||
} |
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,40 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using NWebDav.Server; | ||
using NWebDav.Server.Http; | ||
|
||
namespace YaR.WebDavMailRu.CloudStore.Mailru | ||
{ | ||
public class RequestHandlerFactory : IRequestHandlerFactory | ||
{ | ||
private static readonly IDictionary<string, IRequestHandler> RequestHandlers = new Dictionary<string, IRequestHandler> | ||
{ | ||
{ "COPY", new NWebDav.Server.Handlers.CopyHandler() }, | ||
{ "DELETE", new NWebDav.Server.Handlers.DeleteHandler() }, | ||
{ "GET", new NWebDav.Server.Handlers.GetAndHeadHandler() }, | ||
{ "HEAD", new NWebDav.Server.Handlers.GetAndHeadHandler() }, | ||
{ "LOCK", new NWebDav.Server.Handlers.LockHandler() }, | ||
{ "MKCOL", new NWebDav.Server.Handlers.MkcolHandler() }, | ||
{ "MOVE", new MoveHandler() }, | ||
{ "OPTIONS", new NWebDav.Server.Handlers.OptionsHandler() }, | ||
{ "PROPFIND", new NWebDav.Server.Handlers.PropFindHandler() }, | ||
{ "PROPPATCH", new NWebDav.Server.Handlers.PropPatchHandler() }, | ||
{ "PUT", new NWebDav.Server.Handlers.PutHandler() }, | ||
{ "UNLOCK", new NWebDav.Server.Handlers.UnlockHandler() } | ||
}; | ||
|
||
public IRequestHandler GetRequestHandler(IHttpContext httpContext) | ||
{ | ||
IRequestHandler requestHandler; | ||
if (!RequestHandlers.TryGetValue(httpContext.Request.HttpMethod, out requestHandler)) | ||
return null; | ||
|
||
return requestHandler; | ||
} | ||
|
||
public static IEnumerable<string> AllowedMethods => RequestHandlers.Keys; | ||
} | ||
} |
Oops, something went wrong.