Skip to content

Commit

Permalink
Merge branch 'develop2' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
yar229 committed Dec 2, 2020
2 parents 9288909 + 791ec5a commit cbb8ac2
Show file tree
Hide file tree
Showing 26 changed files with 283 additions and 297 deletions.
7 changes: 3 additions & 4 deletions MailRuCloud/MailRuCloudApi/Base/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,9 @@ protected set

public IEnumerable<PublicLinkInfo> GetPublicLinks(Cloud cloud)
{
if (!PublicLinks.Any())
return cloud.GetSharedLinks(FullPath);

return PublicLinks;
return !PublicLinks.Any()
? cloud.GetSharedLinks(FullPath)
: PublicLinks;
}

/// <summary>
Expand Down
70 changes: 48 additions & 22 deletions MailRuCloud/MailRuCloudApi/Base/FilenameServiceInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Text.RegularExpressions;

namespace YaR.Clouds.Base
{
Expand All @@ -15,7 +14,7 @@ public class FilenameServiceInfo

public override string ToString()
{
return ".wdmrc." + (SplitInfo?.PartNumber.ToString("D3") ?? "000") + (CryptInfo?.AlignBytes.ToString("x") ?? string.Empty);
return WdmrcDots + (SplitInfo?.PartNumber.ToString("D3") ?? "000") + (CryptInfo?.AlignBytes.ToString("x") ?? string.Empty);
}

public string ToString(bool withName)
Expand All @@ -27,33 +26,60 @@ public string ToString(bool withName)

public static FilenameServiceInfo Parse(string filename)
{
var res = new FilenameServiceInfo();


//var malign = Regex.Match(filename, @"\.wdmrc\.(?<partnumber>\d\d\d)(?<align>[0-9a-f])?\Z", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.RightToLeft);

var m = Regex.Match(filename, @"\A(?<cleanname>.*?)(\.wdmrc\.(?<partnumber>\d\d\d)(?<align>[0-9a-f])?)?\Z", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.RightToLeft);
if (!m.Success)
throw new InvalidOperationException("Cannot parse filename");

res.CleanName = m.Groups["cleanname"].Value;

string partnumber = m.Groups["partnumber"].Value;
res.SplitInfo = new FileSplitInfo
{
IsHeader = string.IsNullOrEmpty(partnumber),
PartNumber = string.IsNullOrEmpty(partnumber) ? 0 : int.Parse(m.Groups["partnumber"].Value)
};

string align = m.Groups["align"].Value;
if (!string.IsNullOrEmpty(align))
static int HexToInt(char h)
{
res.CryptInfo = new CryptInfo
return h switch
{
AlignBytes = Convert.ToUInt32(align, 16)
>= '0' and <= '9' => h - '0',
>= 'a' and <= 'f' => h - 'a' + 10,
>= 'A' and <= 'F' => h - 'A' + 10,
_ => -1
};
}

static bool IsDigit(char c) => c >= '0' && c <= '9';


var res = new FilenameServiceInfo { CleanName = filename, SplitInfo = new FileSplitInfo { IsHeader = true } };

if (filename.Length < 11)
return res;

var fns = filename.AsSpan();

int pos = fns.LastIndexOf(WdmrcDots.AsSpan());
if (pos < 0)
return res;

int startpos = pos;

pos += WdmrcDots.Length;
int parselen = fns.Length - pos;

int align = parselen == 4 ? HexToInt(fns[pos + 3]) : -1;
bool hasDigits = (parselen == 3 || (parselen == 4 && align > -1))
&& IsDigit(fns[pos]) && IsDigit(fns[pos + 1]) && IsDigit(fns[pos + 2]);

if (!hasDigits)
return res;

res.CleanName = fns.Slice(0, startpos).ToString();

res.SplitInfo.IsHeader = false;
#if NET48
res.SplitInfo.PartNumber = int.Parse(fns.Slice(pos, 3).ToString());
#else
res.SplitInfo.PartNumber = int.Parse(fns.Slice(pos, 3));
#endif

if (align > -1)
res.CryptInfo = new CryptInfo { AlignBytes = (uint)align };

return res;
}

private const string WdmrcDots = ".wdmrc.";
}
}
7 changes: 3 additions & 4 deletions MailRuCloud/MailRuCloudApi/Base/Folder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ public string FullPath

public IEnumerable<PublicLinkInfo> GetPublicLinks(Cloud cloud)
{
if (!PublicLinks.Any())
return cloud.GetSharedLinks(FullPath);

return PublicLinks;
return !PublicLinks.Any()
? cloud.GetSharedLinks(FullPath)
: PublicLinks;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,42 +138,44 @@ public async Task<IEntry> FolderInfo(RemotePath path, int offset = 0, int limit
var req = new ListRequest(HttpSettings, Authent, _metaServer.Value.Url, path.Path, _listDepth);
var res = await req.MakeRequestAsync();

if (res.Item is FsFolder fsf)
switch (res.Item)
{
var f = new Folder(fsf.Size == null ? 0 : (long)fsf.Size.Value, fsf.FullPath);
foreach (var fsi in fsf.Items)
case FsFolder fsFolder:
{
if (fsi is FsFile fsfi)
var f = new Folder(fsFolder.Size == null ? 0 : (long)fsFolder.Size.Value, fsFolder.FullPath);
foreach (var fsi in fsFolder.Items)
{
var fi = new File(fsfi.FullPath, (long)fsfi.Size, new FileHashMrc(fsfi.Sha1))
if (fsi is FsFile fsfi)
{
CreationTimeUtc = fsfi.ModifDate,
LastWriteTimeUtc = fsfi.ModifDate
};
f.Files.Add(fi);
}
else if (fsi is FsFolder fsfo)
{
var fo = new Folder(fsfo.Size == null ? 0 : (long) fsfo.Size.Value, fsfo.FullPath);
f.Folders.Add(fo);
var fi = new File(fsfi.FullPath, (long)fsfi.Size, new FileHashMrc(fsfi.Sha1))
{
CreationTimeUtc = fsfi.ModifDate,
LastWriteTimeUtc = fsfi.ModifDate
};
f.Files.Add(fi);
}
else if (fsi is FsFolder fsfo)
{
var fo = new Folder(fsfo.Size == null ? 0 : (long) fsfo.Size.Value, fsfo.FullPath);
f.Folders.Add(fo);
}
else throw new Exception($"Unknown item type {fsi.GetType()}");
}
else throw new Exception($"Unknown item type {fsi.GetType()}");
return f;
}
return f;
}

if (res.Item is FsFile fsfi1)
{
var fi = new File(fsfi1.FullPath, (long)fsfi1.Size, new FileHashMrc(fsfi1.Sha1))
case FsFile fsFile:
{
CreationTimeUtc = fsfi1.ModifDate,
LastWriteTimeUtc = fsfi1.ModifDate
};
var fi = new File(fsFile.FullPath, (long)fsFile.Size, new FileHashMrc(fsFile.Sha1))
{
CreationTimeUtc = fsFile.ModifDate,
LastWriteTimeUtc = fsFile.ModifDate
};

return fi;
return fi;
}
default:
return null;
}

return null;
}

public Task<FolderInfoResult> ItemInfo(RemotePath path, int offset = 0, int limit = int.MaxValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,18 @@ private Revision(TreeId treeId, ulong bgn, TreeId newTreeId, ulong newBgn) : thi
public static Revision FromStream(ResponseBodyStream stream)
{
short ver = stream.ReadShort();
switch (ver)
return ver switch
{
case 0:
return new Revision();
case 1:
return new Revision(TreeId.FromStream(stream), stream.ReadULong());
case 2:
return new Revision(TreeId.FromStream(stream), stream.ReadULong());
case 3:
return new Revision(TreeId.FromStream(stream), stream.ReadULong(), TreeId.FromStream(stream), stream.ReadULong());
case 4:
return new Revision(TreeId.FromStream(stream), stream.ReadULong(), TreeId.FromStream(stream), stream.ReadULong());
case 5:
return new Revision(TreeId.FromStream(stream), stream.ReadULong(), TreeId.FromStream(stream));

//more revisions?

default:
throw new Exception("Unknown revision " + ver);
}
0 => new Revision(),
1 => new Revision(TreeId.FromStream(stream), stream.ReadULong()),
2 => new Revision(TreeId.FromStream(stream), stream.ReadULong()),
3 => new Revision(TreeId.FromStream(stream), stream.ReadULong(), TreeId.FromStream(stream),
stream.ReadULong()),
4 => new Revision(TreeId.FromStream(stream), stream.ReadULong(), TreeId.FromStream(stream),
stream.ReadULong()),
5 => new Revision(TreeId.FromStream(stream), stream.ReadULong(), TreeId.FromStream(stream)),
_ => throw new Exception("Unknown revision " + ver)
};
}
}
}
7 changes: 1 addition & 6 deletions MailRuCloud/MailRuCloudApi/Base/Repos/MailRuCloud/OAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ public OAuth(HttpCommonSettings settings, IBasicCredentials creds, AuthCodeRequi

return token;
},
value =>
{
if (null == value)
return TimeSpan.MaxValue;
return value.ExpiresIn.Add(-TimeSpan.FromMinutes(5));
});
value => value?.ExpiresIn.Add(-TimeSpan.FromMinutes(5)) ?? TimeSpan.MaxValue);
//value => TimeSpan.FromSeconds(20));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,9 @@ public async Task<RenameResult> Rename(string fullPath, string newName)

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


return new ShardInfoRequest(HttpSettings, Authent).MakeRequestAsync().Result.ToShardInfo();
return Authent.IsAnonymous
? new Clouds.Base.Repos.MailRuCloud.WebV2.Requests.ShardInfoRequest(HttpSettings, Authent).MakeRequestAsync().Result.ToShardInfo()
: new ShardInfoRequest(HttpSettings, Authent).MakeRequestAsync().Result.ToShardInfo();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,9 @@ public async Task<RenameResult> Rename(string fullPath, string newName)

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


return new ShardInfoRequest(HttpSettings, Authent).MakeRequestAsync().Result.ToShardInfo();
return Authent.IsAnonymous
? new Clouds.Base.Repos.MailRuCloud.WebV2.Requests.ShardInfoRequest(HttpSettings, Authent).MakeRequestAsync().Result.ToShardInfo()
: new ShardInfoRequest(HttpSettings, Authent).MakeRequestAsync().Result.ToShardInfo();
}


Expand Down
20 changes: 6 additions & 14 deletions MailRuCloud/MailRuCloudApi/Base/Repos/RepoFabric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,13 @@ string TwoFaHandler(string login, bool isAutoRelogin)
return code;
}

IRequestRepo repo;
switch (_settings.Protocol)
IRequestRepo repo = _settings.Protocol switch
{
case Protocol.YadWeb:
repo = new YadWebRequestRepo(_settings.Proxy, _credentials);
break;
case Protocol.WebM1Bin:
repo = new WebBinRequestRepo(_settings.Proxy, _credentials, TwoFaHandler);
break;
case Protocol.WebV2:
repo = new WebV2RequestRepo(_settings.Proxy, _credentials, TwoFaHandler);
break;
default:
throw new Exception("Unknown protocol");
}
Protocol.YadWeb => new YadWebRequestRepo(_settings.Proxy, _credentials),
Protocol.WebM1Bin => new WebBinRequestRepo(_settings.Proxy, _credentials, TwoFaHandler),
Protocol.WebV2 => new WebV2RequestRepo(_settings.Proxy, _credentials, TwoFaHandler),
_ => throw new Exception("Unknown protocol")
};

if (!string.IsNullOrWhiteSpace(_settings.UserAgent))
repo.HttpSettings.UserAgent = _settings.UserAgent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,14 @@ public override object ReadJson(JsonReader reader, Type t, object existingValue,
{
if (reader.TokenType == JsonToken.Null) return null;
var value = serializer.Deserialize<string>(reader);
switch (value)

return value switch
{
case "camera":
return YadMediaFilter.Camera;
case "photounlim":
return YadMediaFilter.Photounlim;
case "videos":
return YadMediaFilter.Videos;
}
throw new Exception("Cannot unmarshal type Filter");
"camera" => YadMediaFilter.Camera,
"photounlim" => YadMediaFilter.Photounlim,
"videos" => YadMediaFilter.Videos,
_ => throw new Exception("Cannot unmarshal type Filter")
};
}

public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,24 @@ public async Task<IEntry> FolderInfo(RemotePath path, int offset = 0, int limit


var itdata = itemInfo?.Data;
if (itdata?.Type == null)
return null;

if (itdata.Type == "file")
return itdata.ToFile(PublicBaseUrlDefault);

var entry = folderInfo.Data.ToFolder(itemInfo.Data, resourceStats.Data, path.Path, PublicBaseUrlDefault);

return entry;
switch (itdata?.Type)
{
case null:
return null;
case "file":
return itdata.ToFile(PublicBaseUrlDefault);
default:
{
var entry = folderInfo.Data.ToFolder(itemInfo.Data, resourceStats.Data, path.Path, PublicBaseUrlDefault);
return entry;
}
}
}


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

if (WebDavPath.PathEquals(path, YadMediaPath))
Expand Down
9 changes: 9 additions & 0 deletions MailRuCloud/MailRuCloudApi/Base/Requests/BaseRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ protected virtual HttpWebRequest CreateRequest(string baseDomain = null)
request.ContentType = ConstSettings.DefaultRequestType;
request.Accept = "application/json";
request.UserAgent = Settings.UserAgent;


#if NET48
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
#else
request.AutomaticDecompression = DecompressionMethods.All;
#endif

//request.AllowReadStreamBuffering = true;

return request;
}
Expand Down
Loading

0 comments on commit cbb8ac2

Please sign in to comment.