Skip to content

Commit

Permalink
Merge branch 'develop2'
Browse files Browse the repository at this point in the history
  • Loading branch information
yar229 committed Dec 20, 2018
2 parents 88fc9bf + 376d81c commit 6e3d5a5
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 80 deletions.
11 changes: 7 additions & 4 deletions MailRuCloud/MailRuCloudApi/Base/Auth/WebAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ public WebAuth(HttpCommonSettings settings, IBasicCredentials creds, AuthCodeReq
_authToken = new Cached<AuthTokenResult>(old =>
{
Logger.Debug("AuthToken expired, refreshing.");
var token = Auth().Result;
//_cachedDownloadToken.Expire();
return token;
if (!creds.IsAnonymous)
{
var token = Auth().Result;
return token;
}
return null;
},
value => TimeSpan.FromSeconds(AuthTokenExpiresInSec));

Expand Down Expand Up @@ -86,7 +89,7 @@ public async Task<AuthTokenResult> Auth()
public string Login => _creds.Login;
public string Password => _creds.Password;

public string AccessToken => _authToken.Value.Token;
public string AccessToken => _authToken.Value?.Token;
public string DownloadToken => _cachedDownloadToken.Value;

public void ExpireDownloadToken()
Expand Down
2 changes: 2 additions & 0 deletions MailRuCloud/MailRuCloudApi/Base/Repos/IRequestRepo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
Expand Down Expand Up @@ -48,5 +49,6 @@ interface IRequestRepo
Task<RemoveResult> Remove(string fullPath);

Task<RenameResult> Rename(string fullPath, string newName);
Dictionary<ShardType, ShardInfo> GetShardInfo1();
}
}
9 changes: 9 additions & 0 deletions MailRuCloud/MailRuCloudApi/Base/Repos/MobileRequestRepo.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Mime;
using System.Security.Authentication;
using System.Threading.Tasks;
using YaR.MailRuCloud.Api.Base.Auth;
using YaR.MailRuCloud.Api.Base.Requests;
Expand Down Expand Up @@ -211,6 +213,13 @@ public async Task<RenameResult> Rename(string fullPath, string newName)
return res;
}

public Dictionary<ShardType, ShardInfo> GetShardInfo1()
{
throw new NotImplementedException("Mobile GetShardInfo1 not implemented");
//YaR.MailRuCloud.Api.Base.Requests.WebBin.
//return new ShardInfoRequest(httpsettings, auth).MakeRequestAsync().Result.ToShardInfo();
}

public async Task<CreateFolderResult> CreateFolder(string path)
{
return (await new CreateFolderRequest(HttpSettings, Authent, _metaServer.Value.Url, path).MakeRequestAsync())
Expand Down
7 changes: 5 additions & 2 deletions MailRuCloud/MailRuCloudApi/Base/Repos/ShardManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ShardManager

private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(ShardManager));

public ShardManager(HttpCommonSettings httpsettings, IAuth auth)
public ShardManager(HttpCommonSettings httpsettings, IAuth auth, IRequestRepo repo)
{
_metaServer = new Cached<Requests.WebBin.MobMetaServerRequest.Result>(old =>
{
Expand All @@ -28,7 +28,10 @@ public ShardManager(HttpCommonSettings httpsettings, IAuth auth)
BannedShards = new Cached<List<ShardInfo>>(old => new List<ShardInfo>(),
value => TimeSpan.FromMinutes(2));

CachedShards = new Cached<Dictionary<ShardType, ShardInfo>>(old => new ShardInfoRequest(httpsettings, auth).MakeRequestAsync().Result.ToShardInfo(),
//CachedShards = new Cached<Dictionary<ShardType, ShardInfo>>(old => new ShardInfoRequest(httpsettings, auth).MakeRequestAsync().Result.ToShardInfo(),
// value => TimeSpan.FromSeconds(ShardsExpiresInSec));

CachedShards = new Cached<Dictionary<ShardType, ShardInfo>>(old => repo.GetShardInfo1(),
value => TimeSpan.FromSeconds(ShardsExpiresInSec));

DownloadServersPending = new Pending<Cached<Requests.WebBin.ServerRequest.Result>>(8,
Expand Down
78 changes: 40 additions & 38 deletions MailRuCloud/MailRuCloudApi/Base/Repos/WebM1RequestRepo.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mime;
using System.Security.Authentication;
using System.Threading;
using System.Threading.Tasks;
using YaR.MailRuCloud.Api.Base.Auth;
Expand All @@ -21,24 +23,36 @@ 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;
private readonly IBasicCredentials _creds;
private readonly AuthCodeRequiredDelegate _onAuthCodeRequired;
private readonly int _listDepth;

public HttpCommonSettings HttpSettings { get; } = new HttpCommonSettings
protected ShardManager ShardManager => _shardManager ?? (_shardManager = new ShardManager(HttpSettings, Authent, this));
private ShardManager _shardManager;

protected IRequestRepo AnonymousRepo => _anonymousRepo ??
(_anonymousRepo = new WebV2RequestRepo(HttpSettings.Proxy, _creds,
_onAuthCodeRequired));
private IRequestRepo _anonymousRepo;


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

public WebM1RequestRepo(IWebProxy proxy, IBasicCredentials creds, AuthCodeRequiredDelegate onAuthCodeRequired, int listDepth)
{
_listDepth = listDepth;
_creds = creds;
_onAuthCodeRequired = onAuthCodeRequired;
_listDepth = listDepth;

ServicePointManager.DefaultConnectionLimit = int.MaxValue;

HttpSettings.Proxy = proxy;
Authent = new OAuth(HttpSettings, creds, onAuthCodeRequired);
_shardManager = new ShardManager(HttpSettings, Authent);
//ShardManager = new ShardManager(HttpSettings, Authent);
}

public IAuth Authent { get; }
Expand All @@ -55,8 +69,8 @@ private DownloadStream GetDownloadStreamInternal(File afile, long? start = null,

Cached<Requests.WebBin.ServerRequest.Result> downServer = null;
var pendingServers = isLinked
? _shardManager.WeblinkDownloadServersPending
: _shardManager.DownloadServersPending;
? ShardManager.WeblinkDownloadServersPending
: ShardManager.DownloadServersPending;
Stopwatch watch = new Stopwatch();

HttpWebRequest request = null;
Expand Down Expand Up @@ -155,21 +169,21 @@ public async Task<ShardInfo> GetShardInfo(ShardType shardType)
for (int i = 0; i < 10; i++)
{
Thread.Sleep(80 * i);
var ishards = await Task.Run(() => _shardManager.CachedShards.Value);
var ishards = await Task.Run(() => ShardManager.CachedShards.Value);
var ishard = ishards[shardType];
var banned = _shardManager.BannedShards.Value;
var banned = ShardManager.BannedShards.Value;
if (banned.All(bsh => bsh.Url != ishard.Url))
{
if (refreshed) Authent.ExpireDownloadToken();
return ishard;
}
_shardManager.CachedShards.Expire();
ShardManager.CachedShards.Expire();
refreshed = true;
}

Logger.Error("Cannot get working shard.");

var shards = await Task.Run(() => _shardManager.CachedShards.Value);
var shards = await Task.Run(() => ShardManager.CachedShards.Value);
var shard = shards[shardType];
return shard;
}
Expand All @@ -194,7 +208,7 @@ public async Task<CopyResult> Move(string sourceFullPath, string destinationPath
//var res = req.ToCopyResult();
//return res;

var req = await new Requests.WebBin.MoveRequest(HttpSettings, Authent, _shardManager.MetaServer.Url, sourceFullPath, destinationPath)
var req = await new Requests.WebBin.MoveRequest(HttpSettings, Authent, ShardManager.MetaServer.Url, sourceFullPath, destinationPath)
.MakeRequestAsync();

var res = req.ToCopyResult(WebDavPath.Name(destinationPath));
Expand All @@ -204,29 +218,8 @@ 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;
if (_creds.IsAnonymous)
return await AnonymousRepo.FolderInfo(path, ulink, offset, limit);

FolderInfoResult datares;
try
Expand Down Expand Up @@ -303,19 +296,28 @@ public async Task<RenameResult> Rename(string fullPath, string newName)
//return res;

string newFullPath = WebDavPath.Combine(WebDavPath.Parent(fullPath), newName);
var req = await new Requests.WebBin.MoveRequest(HttpSettings, Authent, _shardManager.MetaServer.Url, fullPath, newFullPath)
var req = await new Requests.WebBin.MoveRequest(HttpSettings, Authent, ShardManager.MetaServer.Url, fullPath, newFullPath)
.MakeRequestAsync();

var res = req.ToRenameResult();
return res;
}

public Dictionary<ShardType, ShardInfo> GetShardInfo1()
{
if (Authent.IsAnonymous)
return new Requests.WebV2.ShardInfoRequest(HttpSettings, Authent).MakeRequestAsync().Result.ToShardInfo();


return new ShardInfoRequest(HttpSettings, Authent).MakeRequestAsync().Result.ToShardInfo();
}

public async Task<CreateFolderResult> CreateFolder(string path)
{
//return (await new CreateFolderRequest(HttpSettings, Authent, path).MakeRequestAsync())
// .ToCreateFolderResult();

return (await new Requests.WebBin.CreateFolderRequest(HttpSettings, Authent, _shardManager.MetaServer.Url, path).MakeRequestAsync())
return (await new Requests.WebBin.CreateFolderRequest(HttpSettings, Authent, ShardManager.MetaServer.Url, path).MakeRequestAsync())
.ToCreateFolderResult();
}

Expand All @@ -328,7 +330,7 @@ public async Task<AddFileResult> AddFile(string fileFullPath, string fileHash, F
//using Mobile request because of supporting file modified time

//TODO: refact, make mixed repo
var req = await new Requests.WebBin.MobAddFileRequest(HttpSettings, Authent, _shardManager.MetaServer.Url, fileFullPath, fileHash, fileSize, dateTime, conflictResolver)
var req = await new Requests.WebBin.MobAddFileRequest(HttpSettings, Authent, ShardManager.MetaServer.Url, fileFullPath, fileHash, fileSize, dateTime, conflictResolver)
.MakeRequestAsync();

var res = req.ToAddFileResult();
Expand Down
15 changes: 9 additions & 6 deletions MailRuCloud/MailRuCloudApi/Base/Repos/WebV2RequestRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,10 @@ public async Task<IEntry> FolderInfo(string path, Link ulink, int offset = 0, in
}

MailRuCloud.ItemType itemType;
if (null == ulink)
itemType = datares.body.home == path
if (null == ulink || ulink.ItemType == MailRuCloud.ItemType.Unknown)
itemType = datares.body.home == path ||
WebDavPath.PathEquals("/" + datares.body.weblink, path)
//datares.body.list.Any(fi => "/" + fi.weblink == path)
? MailRuCloud.ItemType.Folder
: MailRuCloud.ItemType.File;
else
Expand All @@ -245,13 +247,10 @@ public async Task<IEntry> FolderInfo(string path, Link ulink, int offset = 0, in
home: WebDavPath.Parent(path),
ulink: ulink,
filename: ulink == null ? WebDavPath.Name(path) : ulink.OriginalName,
nameReplacement: WebDavPath.Name(path))
nameReplacement: ulink?.IsLinkedToFileSystem ?? true ? WebDavPath.Name(path) : null)
: datares.ToFolder(path, ulink);

return entry;

//var res = req;
//return res;
}

public async Task<FolderInfoResult> ItemInfo(string path, bool isWebLink = false, int offset = 0, int limit = Int32.MaxValue)
Expand Down Expand Up @@ -296,6 +295,10 @@ public async Task<RenameResult> Rename(string fullPath, string newName)
return res;
}

public Dictionary<ShardType, ShardInfo> GetShardInfo1()
{
return new ShardInfoRequest(HttpSettings, Authent).MakeRequestAsync().Result.ToShardInfo();
}


public async Task<Dictionary<ShardType, ShardInfo>> ShardInfo()
Expand Down
25 changes: 14 additions & 11 deletions MailRuCloud/MailRuCloudApi/Base/Requests/WebM1/FolderInfoRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,21 @@ public FolderInfoRequest(HttpCommonSettings settings, IAuth auth, string path, b
_limit = limit;
}

protected override string RelationalUri
{
get
{
var url = $"/api/v2/folder?offset={_offset}&limit={_limit}";
if (!Auth.IsAnonymous)
url += $"access_token={Auth.AccessToken}";
protected override string RelationalUri => $"/api/m1/folder?access_token={Auth.AccessToken}&offset={_offset}&limit={_limit}";

return url;
// $"/api/m1/folder?access_token={Auth.AccessToken}&offset={_offset}&limit={_limit}";
}
}

//protected override string RelationalUri
//{
// get
// {
// var url = $"/api/v2/folder?offset={_offset}&limit={_limit}";
// if (!Auth.IsAnonymous)
// url += $"access_token={Auth.AccessToken}";

// return url;
// // $"/api/m1/folder?access_token={Auth.AccessToken}&offset={_offset}&limit={_limit}";
// }
//}

protected override byte[] CreateHttpContent()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ protected override string RelationalUri
{
get
{
var uri = $"{ConstSettings.CloudDomain}/api/v2/dispatcher?client_id={Settings.ClientId}";
if (!Auth.IsAnonymous)
var uri = $"{ConstSettings.CloudDomain}/api/m1/dispatcher?client_id={Settings.ClientId}";
if (!string.IsNullOrEmpty(Auth.AccessToken))
uri += $"&access_token={Auth.AccessToken}";
else
{
uri += "&email=anonym&x-email=anonym";
}
return uri;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,44 @@ public FolderInfoRequest(HttpCommonSettings settings, IAuth auth, string path, b
_limit = limit;
}

//protected override string RelationalUri
//{
// get
// {
// var uri = _isWebLink
// ? $"/api/v2/folder?token={Auth.AccessToken}&weblink={Uri.EscapeDataString(_path)}&offset={_offset}&limit={_limit}"
// : $"/api/v2/folder?token={Auth.AccessToken}&home={Uri.EscapeDataString(_path)}&offset={_offset}&limit={_limit}";
// return uri;
// }
//}

protected override string RelationalUri
{
get
{
var uri = _isWebLink
? $"/api/v2/folder?token={Auth.AccessToken}&weblink={Uri.EscapeDataString(_path)}&offset={_offset}&limit={_limit}"
: $"/api/v2/folder?token={Auth.AccessToken}&home={Uri.EscapeDataString(_path)}&offset={_offset}&limit={_limit}";
? $"/api/v2/folder?weblink={Uri.EscapeDataString(_path)}&offset={_offset}&limit={_limit}"
: $"/api/v2/folder?home={Uri.EscapeDataString(_path)}&offset={_offset}&limit={_limit}";

if (!Auth.IsAnonymous)
uri += $"&token={Auth.AccessToken}";

return uri;
}
}


//protected override string RelationalUri
//{
// get
// {
// var url = $"/api/v2/folder?offset={_offset}&limit={_limit}";
// if (!Auth.IsAnonymous)
// url += $"access_token={Auth.AccessToken}";

// return url;
// // $"/api/m1/folder?access_token={Auth.AccessToken}&offset={_offset}&limit={_limit}";
// }
//}
}
}
Loading

0 comments on commit 6e3d5a5

Please sign in to comment.