Skip to content

Commit

Permalink
Merge branch 'develop2'
Browse files Browse the repository at this point in the history
  • Loading branch information
yar229 committed Jun 1, 2018
2 parents 900dfcb + 26422a0 commit 570b437
Show file tree
Hide file tree
Showing 20 changed files with 417 additions and 98 deletions.
10 changes: 9 additions & 1 deletion MailRuCloud/MailRuCloudApi/Base/Folder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,20 @@ public string FullPath

public bool IsFile => false;

public PublishInfo ToPublishInfo()
public bool IsChildsLoaded { get; internal set; }

public PublishInfo ToPublishInfo()
{
var info = new PublishInfo();
if (!string.IsNullOrEmpty(PublicLink))
info.Items.Add(new PublishInfoItem { Path = FullPath, Url = ConstSettings.PublishFileLink + PublicLink });
return info;
}


//public List<KeyValuePair<string, IEntry>> GetLinearChilds()
//{

//}
}
}
11 changes: 7 additions & 4 deletions MailRuCloud/MailRuCloudApi/Base/Repos/MobileRequestRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ class MobileRequestRepo : IRequestRepo

//public int PendingDownloads { get; set; }

public MobileRequestRepo(IWebProxy proxy, IAuth auth)
public MobileRequestRepo(IWebProxy proxy, IAuth auth, int listDepth)
{
HttpSettings.Proxy = proxy;
_listDepth = listDepth;

HttpSettings.Proxy = proxy;

Authent = auth;

Expand Down Expand Up @@ -57,7 +59,8 @@ public MobileRequestRepo(IWebProxy proxy, IAuth auth)
private const int MetaServerExpiresSec = 20 * 60;

private readonly Cached<ServerRequest.Result> _downloadServer;
private const int DownloadServerExpiresSec = 20 * 60;
private readonly int _listDepth;
private const int DownloadServerExpiresSec = 20 * 60;


public IAuth Authent { get; }
Expand Down Expand Up @@ -130,7 +133,7 @@ public Task<CopyResult> Move(string sourceFullPath, string destinationPath, Conf

public async Task<IEntry> FolderInfo(string path, Link ulink, int offset = 0, int limit = Int32.MaxValue)
{
var req = new ListRequest(HttpSettings, Authent, _metaServer.Value.Url, path) { Depth = 1};
var req = new ListRequest(HttpSettings, Authent, _metaServer.Value.Url, path, _listDepth);
var res = await req.MakeRequestAsync();

if (res.Item is FsFolder fsf)
Expand Down
2 changes: 1 addition & 1 deletion MailRuCloud/MailRuCloudApi/Base/Repos/RepoFabric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public IRequestRepo Create()
switch (_settings.Protocol)
{
case Protocol.WebM1Bin:
repo = new WebM1RequestRepo(_proxy, _credentials, TwoFaHandler);
repo = new WebM1RequestRepo(_proxy, _credentials, TwoFaHandler, _settings.ListDepth);
break;
case Protocol.WebV2:
repo = new WebV2RequestRepo(_proxy, _credentials, TwoFaHandler);
Expand Down
34 changes: 30 additions & 4 deletions MailRuCloud/MailRuCloudApi/Base/Repos/WebM1RequestRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,27 @@
using YaR.MailRuCloud.Api.Common;
using YaR.MailRuCloud.Api.Extensions;
using YaR.MailRuCloud.Api.Links;
using AccountInfoRequest = YaR.MailRuCloud.Api.Base.Requests.WebM1.AccountInfoRequest;

namespace YaR.MailRuCloud.Api.Base.Repos
{
class WebM1RequestRepo : IRequestRepo
{
private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(WebV2RequestRepo));
private readonly ShardManager _shardManager;
private readonly int _listDepth;


public HttpCommonSettings HttpSettings { get; } = new HttpCommonSettings
public HttpCommonSettings HttpSettings { get; } = new HttpCommonSettings
{
ClientId = "cloud-win",
UserAgent = "CloudDiskOWindows 17.12.0009 beta WzBbt1Ygbm"
};

public WebM1RequestRepo(IWebProxy proxy, IBasicCredentials creds, AuthCodeRequiredDelegate onAuthCodeRequired)
public WebM1RequestRepo(IWebProxy proxy, IBasicCredentials creds, AuthCodeRequiredDelegate onAuthCodeRequired, int listDepth)
{
ServicePointManager.DefaultConnectionLimit = int.MaxValue;
_listDepth = listDepth;

ServicePointManager.DefaultConnectionLimit = int.MaxValue;

HttpSettings.Proxy = proxy;
Authent = new OAuth(HttpSettings, creds, onAuthCodeRequired);
Expand Down Expand Up @@ -201,6 +204,29 @@ public async Task<CopyResult> Move(string sourceFullPath, string destinationPath

public async Task<IEntry> FolderInfo(string path, Link ulink, int offset = 0, int limit = Int32.MaxValue)
{
//IEntry entry;
//try
//{
// //TODO: don't know how to properly get shared links from WebM1Bin proto
// entry = ulink != null
// ? (await new FolderInfoRequest(HttpSettings, Authent, ulink.Href, true, offset, limit)
// .MakeRequestAsync())
// .ToEntry(ulink, path)
// : (await new Requests.WebBin.ListRequest(HttpSettings, Authent, _shardManager.MetaServer.Url, path,
// _listDepth).MakeRequestAsync())
// .ToEntry();
//}
////TODO: refact throwing exceptions from repos
//catch (WebException e) when ((e.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound)
//{
// return null;
//}
//catch (Requests.WebBin.FooWebException e) when (e.StatusCode == HttpStatusCode.NotFound)
//{
// return null;
//}

//return entry;

FolderInfoResult datares;
try
Expand Down
9 changes: 9 additions & 0 deletions MailRuCloud/MailRuCloudApi/Base/Repos/WebV2RequestRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@ public async Task<CopyResult> Move(string sourceFullPath, string destinationPath
return res;
}

/// <summary>
///
/// </summary>
/// <param name="path"></param>
/// <param name="ulink"></param>
/// <param name="offset"></param>
/// <param name="limit"></param>
/// <param name="depth">Not applicable here, always = 1</param>
/// <returns></returns>
public async Task<IEntry> FolderInfo(string path, Link ulink, int offset = 0, int limit = Int32.MaxValue)
{

Expand Down
106 changes: 72 additions & 34 deletions MailRuCloud/MailRuCloudApi/Base/Requests/WebBin/ListRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ internal class ListRequest : BaseRequestMobile<ListRequest.Result>
{
private readonly string _fullPath;

public ListRequest(HttpCommonSettings settings, IAuth auth, string metaServer, string fullPath)
public ListRequest(HttpCommonSettings settings, IAuth auth, string metaServer, string fullPath, int depth)
: base(settings, auth, metaServer)
{
_fullPath = fullPath;
Depth = depth;
}

/// <summary>
/// Folder list depth
/// </summary>
public long Depth { get; set; } = 1;
public long Depth { get; set; }

public Option Options { get; set; } = Option.Unknown128 | Option.Unknown256 | Option.FolderSize | Option.TotalSpace | Option.UsedSpace;

Expand Down Expand Up @@ -68,8 +69,16 @@ protected override byte[] CreateHttpContent()

protected override RequestResponse<Result> DeserializeMessage(ResponseBodyStream data)
{
if (data.OperationResult != OperationResult.Ok)
throw new Exception($"{nameof(ListRequest)} failed with result code {data.OperationResult}");
switch (data.OperationResult)
{
case OperationResult.Ok:
break;
case OperationResult.NotExists:
throw new FooWebException($"{nameof(ListRequest)} failed with result code {data.OperationResult}", HttpStatusCode.NotFound);
default:
throw new Exception($"{nameof(ListRequest)} failed with result code {data.OperationResult}");
}


var res = new Result
{
Expand All @@ -83,9 +92,7 @@ protected override RequestResponse<Result> DeserializeMessage(ResponseBodyStream
res.UsedSpace = data.ReadULong();

res.FingerPrint = data.ReadBytesByLength();

res.Item = Deserialize(data, _fullPath);


return new RequestResponse<Result>
{
Expand All @@ -94,67 +101,85 @@ protected override RequestResponse<Result> DeserializeMessage(ResponseBodyStream
};
}

private enum ParseOp
{
Done = 0,
ReadItem = 1,
Pin = 2,
PinUpper = 3,
Unknown15 = 15
}

private FsItem Deserialize(ResponseBodyStream data, string fullPath)
{
var fakeFolder = new FsFolder(string.Empty, null, CloudFolderType.Generic, null, null);
FsFolder currentFolder = fakeFolder;
fullPath = WebDavPath.Clean(fullPath);

var fakeRoot = new FsFolder(WebDavPath.Parent(fullPath), null, CloudFolderType.Generic, null, null);
FsFolder currentFolder = fakeRoot;
FsFolder lastFolder = null;
int lvl = 0;

int itemStart = data.ReadShort();
while (itemStart != 0)
var parseOp = (ParseOp)data.ReadShort();
while (parseOp != ParseOp.Done)
{
switch (itemStart)
switch (parseOp)
{
case 1:
case ParseOp.ReadItem:
break;

case 2:
case ParseOp.Pin:
if (lastFolder != null)
{
currentFolder = lastFolder;
itemStart = data.ReadShort();
lvl++;
parseOp = (ParseOp)data.ReadShort();
continue;
}
else
throw new Exception("lastFolder = null");

case 3:
if (currentFolder == fakeFolder)
case ParseOp.PinUpper:
if (currentFolder == fakeRoot)
{
itemStart = data.ReadShort();
parseOp = (ParseOp)data.ReadShort();
continue;
}
else if (currentFolder.Parent != null)
{
{
currentFolder = currentFolder.Parent;
if (currentFolder == null)
throw new Exception("No parent folder");

lvl--;
parseOp = (ParseOp)data.ReadShort();
if (currentFolder == null)
throw new Exception("No parent folder A");
continue;
}
else
throw new Exception("No parent folder");
throw new Exception("No parent folder B");

case 15:
var skip = data.ReadPu32();
for (int i = 0; i < skip; i++)
case ParseOp.Unknown15:
long skip = data.ReadPu32();
for (long i = 0; i < skip; i++)
{
data.ReadPu32();
data.ReadPu32();
}
break;
default:
throw new Exception("Unknown itemStart");
throw new Exception("Unknown parse operation");
}
FsItem item = GetItem(data, currentFolder);
FsItem item = GetItem(data, currentFolder);
currentFolder.Items.Add(item);

if (item is FsFolder fsFolder) lastFolder = fsFolder;
if (item is FsFolder fsFolder)
{
lastFolder = fsFolder;
fsFolder.IsChildsLoaded = lvl < Depth;
}

itemStart = data.ReadShort();
parseOp = (ParseOp)data.ReadShort();
}

var res = fakeFolder.Items[0];
var res = fakeRoot.Items[0];
return res;
}

Expand Down Expand Up @@ -194,16 +219,17 @@ void ProcessDelete()
data.ReadULong(); // dunno
ProcessDelete();

item = new FsFolder(WebDavPath.Combine(_fullPath, name), treeId, CloudFolderType.MountPoint, folder, GetFolderSize());
item = new FsFolder(WebDavPath.Combine(folder.FullPath, name), treeId, CloudFolderType.MountPoint, folder, GetFolderSize());
break;

case 1:
var modifDate = data.ReadDate();
ulong size = data.ReadULong();
byte[] sha1 = data.ReadNBytes(20);

item = new FsFile(WebDavPath.Combine(folder.FullPath, name), modifDate, sha1, size);
break;
//item = new FsFile(WebDavPath.Combine(folder.FullPath == string.Empty ? _fullPath : folder.FullPath, name), modifDate, sha1, size);
item = new FsFile(WebDavPath.Combine(folder.FullPath, name), modifDate, sha1, size);
break;

case 2:
data.ReadULong(); // dunno
Expand Down Expand Up @@ -236,7 +262,19 @@ public class Result : RevisionResponseResult
public byte[] FingerPrint { get; set; }

public FsItem Item { get; set; }

}
}

//TODO: refact with WebException?
internal class FooWebException : Exception
{
public FooWebException(string message, HttpStatusCode statusCode)
{
Message = message;
StatusCode = statusCode;
}

public string Message { get; }
public HttpStatusCode StatusCode { get; }
}
}
Loading

0 comments on commit 570b437

Please sign in to comment.