Skip to content

Commit

Permalink
Merge branch 'develop2'
Browse files Browse the repository at this point in the history
  • Loading branch information
yar229 committed Oct 11, 2022
2 parents ac8c056 + 29ed710 commit 942def1
Show file tree
Hide file tree
Showing 92 changed files with 746 additions and 826 deletions.
18 changes: 9 additions & 9 deletions Hasher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ private static void Main(string[] args)

foreach (string filelist in options.Filelists)
{
if (!string.IsNullOrEmpty(filelist))
if (string.IsNullOrEmpty(filelist))
continue;

if (!File.Exists(filelist))
throw new FileNotFoundException($"List file not found {filelist}");
using (TextReader reader = File.OpenText(filelist))
{
if (!File.Exists(filelist))
throw new FileNotFoundException($"List file not found {filelist}");
using (TextReader reader = File.OpenText(filelist))
{
string line;
while (!string.IsNullOrEmpty(line = reader.ReadLine()))
cards.Add(line);
}
string line;
while (!string.IsNullOrEmpty(line = reader.ReadLine()))
cards.Add(line);
}
}

Expand Down
24 changes: 10 additions & 14 deletions MailRuCloud/MailRuCloudApi/Base/CryptoUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,21 @@ public static class CryptoUtil
{
public static byte[] GetCryptoKey(string password, byte[] salt)
{
using (var keygen = new Rfc2898DeriveBytes(password, salt, 4002))
{
var key = keygen.GetBytes(32);
return key;
}
using var keygen = new Rfc2898DeriveBytes(password, salt, 4002);
var key = keygen.GetBytes(32);
return key;
}

public static KeyAndSalt GetCryptoKeyAndSalt(string password, int saltSize = SaltSizeInBytes)
{
using (var keygen = new Rfc2898DeriveBytes(password, saltSize, 4002))
using var keygen = new Rfc2898DeriveBytes(password, saltSize, 4002);
var res = new KeyAndSalt
{
var res = new KeyAndSalt
{
Salt = keygen.Salt,
Key = keygen.GetBytes(32),
IV = keygen.GetBytes(32)
};
return res;
}
Salt = keygen.Salt,
Key = keygen.GetBytes(32),
IV = keygen.GetBytes(32)
};
return res;
}

public static CryptoKeyInfo GetCryptoPublicInfo(Cloud cloud, File file)
Expand Down
41 changes: 22 additions & 19 deletions MailRuCloud/MailRuCloudApi/Base/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public File(string fullPath, long size, IFileHash hash = null)
_hash = hash;
}

public File(string fullPath, long size, params PublicLinkInfo[] links)
: this(fullPath, size)
{
PublicLinks.AddRange(links);
}

private IFileHash _hash;

Expand Down Expand Up @@ -91,7 +96,7 @@ public virtual FileSize OriginalSize
}
private FileSize _originalSize;

protected virtual File FileHeader { get; } = null;
protected virtual File FileHeader => null;

/// <summary>
/// Gets full file path with name on server.
Expand Down Expand Up @@ -151,36 +156,34 @@ public void SetName(string destinationName)
FullPath = WebDavPath.Combine(Path, destinationName);
if (ServiceInfo != null) ServiceInfo.CleanName = Name;

if (Files.Count > 1)
{
string path = Path;
foreach (var fiFile in Parts)
{
fiFile.FullPath = WebDavPath.Combine(path, destinationName + fiFile.ServiceInfo.ToString(false)); //TODO: refact
}
}
if (Files.Count <= 1)
return;

string path = Path;
foreach (var fiFile in Parts)
fiFile.FullPath = WebDavPath.Combine(path, destinationName + fiFile.ServiceInfo.ToString(false)); //TODO: refact
}

//TODO : refact, bad design
public void SetPath(string fullPath)
{
FullPath = WebDavPath.Combine(fullPath, Name);
if (Parts.Count > 1)
foreach (var fiFile in Parts)
{
fiFile.FullPath = WebDavPath.Combine(fullPath, fiFile.Name); //TODO: refact
}
if (Parts.Count <= 1)
return;

foreach (var fiFile in Parts)
fiFile.FullPath = WebDavPath.Combine(fullPath, fiFile.Name); //TODO: refact
}


//TODO : refact, bad design
public CryptoKeyInfo EnsurePublicKey(Cloud cloud)
{
if (ServiceInfo.IsCrypted && null == ServiceInfo.CryptInfo.PublicKey)
{
var info = cloud.DownloadFileAsJson<HeaderFileContent>(FileHeader ?? this);
ServiceInfo.CryptInfo.PublicKey = info.PublicKey;
}
if (!ServiceInfo.IsCrypted || null != ServiceInfo.CryptInfo.PublicKey)
return ServiceInfo.CryptInfo.PublicKey;

var info = cloud.DownloadFileAsJson<HeaderFileContent>(FileHeader ?? this);
ServiceInfo.CryptInfo.PublicKey = info.PublicKey;
return ServiceInfo.CryptInfo.PublicKey;
}

Expand Down
18 changes: 6 additions & 12 deletions MailRuCloud/MailRuCloudApi/Base/FileSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@ namespace YaR.Clouds.Base
{
public FileSize(long defaultValue) : this()
{
_defValue = defaultValue;
DefaultValue = defaultValue;
}

/// <summary>
/// Private variable for default value.
/// </summary>
private readonly long _defValue;

/// <summary>
/// Gets default size in bytes.
/// </summary>
/// <value>File size.</value>
public long DefaultValue => _defValue; //TODO: make it ulong
public long DefaultValue { get; } //TODO: make it ulong


#region == Equality ===================================================================================================================
Expand Down Expand Up @@ -54,21 +49,20 @@ public static implicit operator ulong(FileSize fsize)

public bool Equals(FileSize other)
{
return _defValue == other._defValue;
return DefaultValue == other.DefaultValue;
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
return false;
if (obj.GetType() != GetType())
return false;
return Equals((FileSize)obj);

return obj.GetType() == GetType() && Equals((FileSize)obj);
}

public override int GetHashCode()
{
return _defValue.GetHashCode();
return DefaultValue.GetHashCode();
}
#endregion == Equality ===================================================================================================================
}
Expand Down
2 changes: 1 addition & 1 deletion MailRuCloud/MailRuCloudApi/Base/FilenameServiceInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static int HexToInt(char h)
if (!hasDigits)
return res;

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

res.SplitInfo.IsHeader = false;
#if NET48
Expand Down
8 changes: 3 additions & 5 deletions MailRuCloud/MailRuCloudApi/Base/Repos/IRequestRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ private RemotePath()
public static async Task<RemotePath> Get(string path, LinkManager lm)
{
var z = new RemotePath {Path = path};
if (lm != null)
{
var lnk = await lm.GetItemLink(path);
z.Link = lnk;
}
if (lm == null)
return z;

z.Link = await lm.GetItemLink(path);
return z;
}

Expand Down
30 changes: 14 additions & 16 deletions MailRuCloud/MailRuCloudApi/Base/Repos/MailRuCloud/DtoImport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,22 @@ public static UploadFileResult ToUploadPathResult(this HttpResponseMessage respo
{
var res = new UploadFileResult { HttpStatusCode = response.StatusCode, HasReturnedData = false };

if (response.IsSuccessStatusCode)
{
var strres = response.Content.ReadAsStringAsync().Result;
if (!response.IsSuccessStatusCode)
return res;

var strres = response.Content.ReadAsStringAsync().Result;

if (string.IsNullOrEmpty(strres))
return res;
if (string.IsNullOrEmpty(strres))
return res;

res.HasReturnedData = true;
res.HasReturnedData = true;

var resp = strres.Split(';');
var resp = strres.Split(';');

res.Hash = new FileHashMrc(resp[0]);
res.Size = resp.Length > 1
? long.Parse(resp[1].Trim('\r', '\n', ' '))
: 0;
}
res.Hash = new FileHashMrc(resp[0]);
res.Size = resp.Length > 1
? long.Parse(resp[1].Trim('\r', '\n', ' '))
: 0;

return res;
}
Expand Down Expand Up @@ -289,11 +289,9 @@ public static File ToFile(this FolderInfoResult data, string publicBaseUrl, stri
cmpname = WebDavPath.Name(ulink.PublicLinks.First().Uri.OriginalString);
}

var groupedFile = z?
.ToGroupedFiles();

var groupedFile = z?.ToGroupedFiles();

var res = groupedFile.First(it => string.IsNullOrEmpty(cmpname) || it.Name == cmpname);
var res = groupedFile?.First(it => string.IsNullOrEmpty(cmpname) || it.Name == cmpname);

return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ protected MailRuBaseRepo(IBasicCredentials creds)
{
Credentials = creds;

if (!AvailDomains.Any(d => creds.Login.Contains($"@{d}.")))
{
string domains = AvailDomains.Aggregate((c, n) => c + ", @" + n);
Logger.Warn($"Missing domain part ({domains}) in login, file and folder deleting will be denied");
}
if (AvailDomains.Any(d => creds.Login.Contains($"@{d}.")))
return;

string domains = AvailDomains.Aggregate((c, n) => c + ", @" + n);
Logger.Warn($"Missing domain part ({domains}) in login, file and folder deleting will be denied");
}

protected readonly IBasicCredentials Credentials;
Expand Down
22 changes: 11 additions & 11 deletions MailRuCloud/MailRuCloudApi/Base/Repos/MailRuCloud/MailRuSha1Hash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ public IFileHash Hash
{
get
{
if (null == _hash)
if (null != _hash)
return new FileHashMrc(_hash);

if (_length <= 20)
_hash = _smallContent;
else
{
if (_length <= 20)
_hash = _smallContent;
else
{
AppendFinalBuffer();

_sha1.TransformFinalBlock(Array.Empty<byte>(), 0, 0);
_hash = _sha1.Hash;
}
_isClosed = true;
AppendFinalBuffer();

_sha1.TransformFinalBlock(Array.Empty<byte>(), 0, 0);
_hash = _sha1.Hash;
}
_isClosed = true;
return new FileHashMrc(_hash);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ public CreateFolderRequest(HttpCommonSettings settings, IAuth auth, string metaS

protected override byte[] CreateHttpContent()
{
using (var stream = new RequestBodyStream())
{
stream.WritePu16((byte)Operation.CreateFolder);
stream.WritePu16(Revision);
stream.WriteString(_fullPath);
stream.WritePu32(0);
var body = stream.GetBytes();
return body;
}
using var stream = new RequestBodyStream();

stream.WritePu16((byte)Operation.CreateFolder);
stream.WritePu16(Revision);
stream.WriteString(_fullPath);
stream.WritePu32(0);
var body = stream.GetBytes();
return body;
}

protected override RequestResponse<Result> DeserializeMessage(NameValueCollection responseHeaders, ResponseBodyStream data)
Expand Down
Loading

0 comments on commit 942def1

Please sign in to comment.