Skip to content

Commit

Permalink
Merge branch 'develop2'
Browse files Browse the repository at this point in the history
  • Loading branch information
yar229 committed Apr 13, 2020
2 parents fbf60a7 + 6cacbc2 commit 9cfc37f
Show file tree
Hide file tree
Showing 20 changed files with 214 additions and 107 deletions.
25 changes: 20 additions & 5 deletions MailRuCloud/MailRuCloudApi/Base/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public File(string fullPath, long size, string hash = "")
}


private string _fullPath;
private string _hash;

/// <summary>
Expand All @@ -52,12 +51,22 @@ public virtual File New(string newfullPath)
/// Gets file name.
/// </summary>
/// <value>File name.</value>
public virtual string Name => WebDavPath.Name(FullPath);
public string Name
{
get => _name;
private set
{
_name = value;

Extension = System.IO.Path.GetExtension(_name)?.TrimStart('.') ?? string.Empty;
}
} //WebDavPath.Name(FullPath)
private string _name;

/// <summary>
/// Gets file extension
/// Gets file extension (without ".")
/// </summary>
public string Extension => System.IO.Path.GetExtension(Name);
public string Extension { get; private set; }

/// <summary>
/// Gets file hash value.
Expand Down Expand Up @@ -91,9 +100,15 @@ public virtual FileSize OriginalSize
public string FullPath
{
get => _fullPath;
protected set => _fullPath = WebDavPath.Clean(value);
protected set
{
_fullPath = WebDavPath.Clean(value);
Name = WebDavPath.Name(_fullPath);
}
}

private string _fullPath;

/// <summary>
/// Path to file (without filename)
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions MailRuCloud/MailRuCloudApi/Base/Folder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public IEnumerable<IEntry> Entries
/// Gets number of folders in folder.
/// </summary>
/// <value>Number of folders.</value>
public int NumberOfFolders => Entries.OfType<Folder>().Count();
public int NumberOfFolders => Files.Count;

/// <summary>
/// Gets number of files in folder.
/// </summary>
/// <value>Number of files.</value>
public int NumberOfFiles => Entries.OfType<File>().Count();
public int NumberOfFiles => Folders.Count;

/// <summary>
/// Gets folder name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public static IEntry ToFolder(this YadFolderInfoRequestData data, YadItemInfoReq
{
var fi = data.Resources;

var res = new Folder(itemInfo.Meta.Size, path) { IsChildsLoaded = true };
if (!string.IsNullOrEmpty(itemInfo.Meta.UrlShort))
var res = new Folder(itemInfo?.Meta?.Size ?? 0, path) { IsChildsLoaded = true };
if (!string.IsNullOrEmpty(itemInfo?.Meta?.UrlShort))
res.PublicLinks.Add(new PublicLinkInfo("short", itemInfo.Meta.UrlShort));

res.Files.AddRange(fi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ namespace YaR.Clouds.Base.Repos.YandexDisk.YadWeb.Models
{
class YadItemInfoPostModel : YadPostModel
{
public YadItemInfoPostModel(string path)
private readonly string _prefix;

public YadItemInfoPostModel(string path, string prefix = "/disk")
{
_prefix = prefix;
Name = "resource";
Path = path;
}
Expand All @@ -18,7 +21,7 @@ public override IEnumerable<KeyValuePair<string, string>> ToKvp(int index)
foreach (var pair in base.ToKvp(index))
yield return pair;

yield return new KeyValuePair<string, string>($"id.{index}", WebDavPath.Combine("/disk/", Path));
yield return new KeyValuePair<string, string>($"id.{index}", WebDavPath.Combine(_prefix, Path));

//if (Path == "/Camera")
//{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using YaR.Clouds.Base.Repos.MailRuCloud;
using YaR.Clouds.Base.Repos.YandexDisk.YadWeb.Models;
Expand Down Expand Up @@ -157,7 +156,7 @@ public async Task<IEntry> FolderInfo(RemotePath path, int offset = 0, int limit
throw new NotImplementedException(nameof(FolderInfo));

if (path.Path.StartsWith(YadMediaPath))
return await MediaFolderRootInfo();
return await MediaFolderInfo(path.Path);

// YaD perform async deletion
YadResponseModel<YadItemInfoRequestData, YadItemInfoRequestParams> itemInfo = null;
Expand Down Expand Up @@ -205,6 +204,31 @@ public async Task<IEntry> FolderInfo(RemotePath path, int offset = 0, int limit
}


private async Task<IEntry> MediaFolderInfo(string path)
{
var root = await MediaFolderRootInfo() as Folder;
if (null == root)
return null;

if (WebDavPath.PathEquals(path, YadMediaPath))
return root;

string albumName = WebDavPath.Name(path);
var album = root.Folders.FirstOrDefault(f => f.Name == albumName);
if (null == album)
return null;

_ = new YaDCommonRequest(HttpSettings, (YadWebAuth) Authent)
.With(new YadFolderInfoPostModel(album.PublicLinks.First().Key, "/album"),
out YadResponseModel<YadFolderInfoRequestData, YadFolderInfoRequestParams> folderInfo)
.MakeRequestAsync()
.Result;

var entry = folderInfo.Data.ToFolder(null, path, PublicBaseUrlDefault);

return entry;
}

private async Task<IEntry> MediaFolderRootInfo()
{
var res = new Folder(YadMediaPath);
Expand Down
4 changes: 2 additions & 2 deletions MailRuCloud/MailRuCloudApi/YaR.Clouds.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<TargetFrameworks>netcoreapp3.0;net48</TargetFrameworks>
<RootNamespace>YaR.Clouds</RootNamespace>
<AssemblyName>YaR.Clouds</AssemblyName>
<AssemblyVersion>1.11.0.16</AssemblyVersion>
<FileVersion>1.11.0.16</FileVersion>
<AssemblyVersion>1.11.0.19</AssemblyVersion>
<FileVersion>1.11.0.19</FileVersion>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

Expand Down
18 changes: 9 additions & 9 deletions NWebDav/NWebDav.Server/Handlers/LockHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,29 +91,29 @@ public async Task<bool> HandleRequestAsync(IHttpContext httpContext, IStore stor
throw new Exception("No root element (expected 'lockinfo')");

// The document should contain a 'lockinfo' element
if (xRoot.Name != WebDavNamespaces.DavNs + "lockinfo")
if (xRoot.Name != WebDavNamespaces.DavNsLockInfo)
throw new Exception("Invalid root element (expected 'lockinfo')");

// Check all descendants
var xLockScope = xRoot.Elements(WebDavNamespaces.DavNs + "lockscope").Single();
var xLockScope = xRoot.Elements(WebDavNamespaces.DavNsLockScope).Single();
var xLockScopeValue = xLockScope.Elements().Single();
if (xLockScopeValue.Name == WebDavNamespaces.DavNs + "exclusive")
if (xLockScopeValue.Name == WebDavNamespaces.DavNsExclusive)
lockScope = LockScope.Exclusive;
else if (xLockScopeValue.Name == WebDavNamespaces.DavNs + "shared")
else if (xLockScopeValue.Name == WebDavNamespaces.DavNsShared)
lockScope = LockScope.Shared;
else
throw new Exception("Invalid lockscope (expected 'exclusive' or 'shared')");

// Determine the lock-type
var xLockType = xRoot.Elements(WebDavNamespaces.DavNs + "locktype").Single();
var xLockType = xRoot.Elements(WebDavNamespaces.DavNsLockType).Single();
var xLockTypeValue = xLockType.Elements().Single();
if (xLockTypeValue.Name == WebDavNamespaces.DavNs + "write")
if (xLockTypeValue.Name == WebDavNamespaces.DavNsWrite)
lockType = LockType.Write;
else
throw new Exception("Invalid locktype (expected 'write')");

// Determine the owner
var xOwner = xRoot.Elements(WebDavNamespaces.DavNs + "owner").Single();
var xOwner = xRoot.Elements(WebDavNamespaces.DavNsOwner).Single();
owner = xOwner.Elements().Single();
}
catch (Exception)
Expand All @@ -139,8 +139,8 @@ public async Task<bool> HandleRequestAsync(IHttpContext httpContext, IStore stor

// Return the information about the lock
var xDocument = new XDocument(
new XElement(WebDavNamespaces.DavNs + "prop",
new XElement(WebDavNamespaces.DavNs + "lockdiscovery",
new XElement(WebDavNamespaces.DavNsProp,
new XElement(WebDavNamespaces.DavNsLockDiscovery,
lockResult.Lock.Value.ToXml())));

// Add the Lock-Token in the response
Expand Down
46 changes: 23 additions & 23 deletions NWebDav/NWebDav.Server/Handlers/PropFindHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public async Task<bool> HandleRequestAsync(IHttpContext httpContext, IStore stor
}

// Obtain the status document
var xMultiStatus = new XElement(WebDavNamespaces.DavNs + "multistatus");
var xMultiStatus = new XElement(WebDavNamespaces.DavNsMultiStatus);
var xDocument = new XDocument(xMultiStatus);

object locker = new object();
Expand All @@ -131,17 +131,17 @@ public async Task<bool> HandleRequestAsync(IHttpContext httpContext, IStore stor
string href = entry.Uri.PathEncoded;

// fusedav 0.2 differs files from folders using ending '/'
bool isCollection = entry.Entry is IStoreCollection;
href = isCollection
? href.EndsWith("/") ? href : href + "/"
: href.TrimEnd('/');
//bool isCollection = entry.Entry is IStoreCollection;
//href = isCollection
// ? href.EndsWith("/") ? href : href + "/"
// : href.TrimEnd('/');

// Create the property
var xResponse = new XElement(WebDavNamespaces.DavNs + "response",
new XElement(WebDavNamespaces.DavNs + "href", href));
var xResponse = new XElement(WebDavNamespaces.DavNsResponse,
new XElement(WebDavNamespaces.DavNsHref, href));

// Create tags for property values
var xPropStatValues = new XElement(WebDavNamespaces.DavNs + "propstat");
var xPropStatValues = new XElement(WebDavNamespaces.DavNsPropStat);

// Check if the entry supports properties
var propertyManager = entry.Entry.PropertyManager;
Expand Down Expand Up @@ -180,7 +180,7 @@ public async Task<bool> HandleRequestAsync(IHttpContext httpContext, IStore stor
}

// Add the status
xPropStatValues.Add(new XElement(WebDavNamespaces.DavNs + "status", "HTTP/1.1 200 OK"));
xPropStatValues.Add(new XElement(WebDavNamespaces.DavNsStatus, "HTTP/1.1 200 OK"));

lock (locker)
{
Expand Down Expand Up @@ -213,10 +213,10 @@ private async Task AddPropertyAsync(IHttpContext httpContext, XElement xResponse
value = ((IEnumerable<XElement>) value).Cast<object>().ToArray();

// Make sure we use the same 'prop' tag to add all properties
var xProp = xPropStatValues.Element(WebDavNamespaces.DavNs + "prop");
var xProp = xPropStatValues.Element(WebDavNamespaces.DavNsProp);
if (xProp == null)
{
xProp = new XElement(WebDavNamespaces.DavNs + "prop");
xProp = new XElement(WebDavNamespaces.DavNsProp);
xPropStatValues.Add(xProp);
}

Expand All @@ -227,19 +227,19 @@ private async Task AddPropertyAsync(IHttpContext httpContext, XElement xResponse
//spam on each file...
//s_log.Log(LogLevel.Warning, () => $"Property {propertyName} is not supported on item {item.Name}.");

xResponse.Add(new XElement(WebDavNamespaces.DavNs + "propstat",
new XElement(WebDavNamespaces.DavNs + "prop", new XElement(propertyName, null)),
new XElement(WebDavNamespaces.DavNs + "status", "HTTP/1.1 404 Not Found"),
new XElement(WebDavNamespaces.DavNs + "responsedescription", $"Property {propertyName} is not supported.")));
xResponse.Add(new XElement(WebDavNamespaces.DavNsPropStat,
new XElement(WebDavNamespaces.DavNsProp, new XElement(propertyName, null)),
new XElement(WebDavNamespaces.DavNsStatus, "HTTP/1.1 404 Not Found"),
new XElement(WebDavNamespaces.DavNsResponseDescription, $"Property {propertyName} is not supported.")));
}
}
catch (Exception exc)
{
s_log.Log(LogLevel.Error, () => $"Property {propertyName} on item {item.Name} raised an exception.", exc);
xResponse.Add(new XElement(WebDavNamespaces.DavNs + "propstat",
new XElement(WebDavNamespaces.DavNs + "prop", new XElement(propertyName, null)),
new XElement(WebDavNamespaces.DavNs + "status", "HTTP/1.1 500 Internal server error"),
new XElement(WebDavNamespaces.DavNs + "responsedescription", $"Property {propertyName} on item {item.Name} raised an exception.")));
xResponse.Add(new XElement(WebDavNamespaces.DavNsPropStat,
new XElement(WebDavNamespaces.DavNsProp, new XElement(propertyName, null)),
new XElement(WebDavNamespaces.DavNsStatus, "HTTP/1.1 500 Internal server error"),
new XElement(WebDavNamespaces.DavNsResponseDescription, $"Property {propertyName} on item {item.Name} raised an exception.")));
}
}
}
Expand All @@ -248,7 +248,7 @@ private static PropertyMode GetRequestedProperties(IHttpRequest request, ICollec
{
// Create an XML document from the stream
var xDocument = request.LoadXmlDocument();
if (xDocument?.Root == null || xDocument.Root.Name != WebDavNamespaces.DavNs + "propfind")
if (xDocument?.Root == null || xDocument.Root.Name != WebDavNamespaces.DavNsPropFind)
return PropertyMode.AllProperties;

// Obtain the propfind node
Expand All @@ -264,15 +264,15 @@ private static PropertyMode GetRequestedProperties(IHttpRequest request, ICollec
foreach (var xProp in xPropFind.Elements())
{
// Check if we should fetch all property names
if (xProp.Name == WebDavNamespaces.DavNs + "propname")
if (xProp.Name == WebDavNamespaces.DavNsPropName)
{
propertyMode = PropertyMode.PropertyNames;
}
else if (xProp.Name == WebDavNamespaces.DavNs + "allprop")
else if (xProp.Name == WebDavNamespaces.DavNsAllProp)
{
propertyMode = PropertyMode.AllProperties;
}
else if (xProp.Name == WebDavNamespaces.DavNs + "include")
else if (xProp.Name == WebDavNamespaces.DavNsInclude)
{
// Include properties
propertyMode = PropertyMode.AllProperties | PropertyMode.SelectedProperties;
Expand Down
18 changes: 9 additions & 9 deletions NWebDav/NWebDav.Server/Handlers/PropPatchHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public PropSet(XName name, object value)
public XElement GetXmlResponse()
{
var statusText = $"HTTP/1.1 {(int)Result} {Result.GetStatusDescription()}";
return new XElement(WebDavNamespaces.DavNs + "propstat",
new XElement(WebDavNamespaces.DavNs + "prop", new XElement(Name)),
new XElement(WebDavNamespaces.DavNs + "status", statusText));
return new XElement(WebDavNamespaces.DavNsPropStat,
new XElement(WebDavNamespaces.DavNsProp, new XElement(Name)),
new XElement(WebDavNamespaces.DavNsStatus, statusText));
}
}

Expand All @@ -55,26 +55,26 @@ public PropSetCollection(IHttpRequest request)
var xRoot = xDoc.Root;
if (xRoot == null)
throw new Exception("No root element (expected 'propertyupdate')");
if (xRoot.Name != WebDavNamespaces.DavNs + "propertyupdate")
if (xRoot.Name != WebDavNamespaces.DavNsPropertyUpdate)
throw new Exception("Invalid root element (expected 'propertyupdate')");

// Check all descendants
foreach (var xElement in xRoot.Elements())
{
// The descendant should be a 'set' or 'remove' entry
if (xElement.Name != WebDavNamespaces.DavNs + "set" && xElement.Name != WebDavNamespaces.DavNs + "remove")
if (xElement.Name != WebDavNamespaces.DavNsSet && xElement.Name != WebDavNamespaces.DavNsRemove)
throw new Exception("Expected 'set' or 'remove' entry");

// Obtain the properties
foreach (var xProperty in xElement.Descendants(WebDavNamespaces.DavNs + "prop"))
foreach (var xProperty in xElement.Descendants(WebDavNamespaces.DavNsProp))
{
// Determine the actual property element
var xActualProperty = xProperty.Elements().FirstOrDefault();
if (xActualProperty != null)
{
// Determine the new property value
object newValue;
if (xElement.Name == WebDavNamespaces.DavNs + "set")
if (xElement.Name == WebDavNamespaces.DavNsSet)
{
// If the descendant is XML, then use the XElement, otherwise use the string
newValue = xActualProperty.HasElements ? (object)xActualProperty.Elements().FirstOrDefault() : xActualProperty.Value;
Expand All @@ -93,8 +93,8 @@ public PropSetCollection(IHttpRequest request)

public XElement GetXmlMultiStatus(WebDavUri uri)
{
var xResponse = new XElement(WebDavNamespaces.DavNs + "response", new XElement(WebDavNamespaces.DavNs + "href", uri));
var xMultiStatus = new XElement(WebDavNamespaces.DavNs + "multistatus", xResponse);
var xResponse = new XElement(WebDavNamespaces.DavNsResponse, new XElement(WebDavNamespaces.DavNsHref, uri));
var xMultiStatus = new XElement(WebDavNamespaces.DavNsMultiStatus, xResponse);
foreach (var result in _propertySetters.Where(ps => ps.Result != DavStatusCode.Ok))
xResponse.Add(result.GetXmlResponse());
return xMultiStatus;
Expand Down
Loading

0 comments on commit 9cfc37f

Please sign in to comment.