Skip to content

Commit

Permalink
code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
yar229 committed Oct 10, 2022
1 parent c005870 commit a4da3a6
Show file tree
Hide file tree
Showing 29 changed files with 190 additions and 270 deletions.
5 changes: 5 additions & 0 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
13 changes: 4 additions & 9 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,7 +49,7 @@ public FileSize(long defaultValue) : this()

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

public override bool Equals(object obj)
Expand All @@ -67,7 +62,7 @@ public override bool Equals(object 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
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,10 @@ private FsItem Deserialize(ResponseBodyStream data, string fullPath)
break;

case ParseOp.Pin:
if (lastFolder != null)
{
currentFolder = lastFolder;
lvl++;
parseOp = (ParseOp)data.ReadShort();
continue;
}

throw new Exception("lastFolder = null");
currentFolder = lastFolder ?? throw new Exception("lastFolder = null");
lvl++;
parseOp = (ParseOp)data.ReadShort();
continue;

case ParseOp.PinUpper:
if (currentFolder == fakeRoot)
Expand All @@ -145,17 +140,12 @@ private FsItem Deserialize(ResponseBodyStream data, string fullPath)
continue;
}

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

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

case ParseOp.Unknown15:
long skip = data.ReadPu32();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public async Task<CreateFolderResult> CreateFolder(string path)
.ToCreateFolderResult();
}

public async Task<AddFileResult> AddFile(string fileFullPath, IFileHash fileHash, FileSize fileSize, DateTime dateTime, ConflictResolver? conflictResolver)
public Task<AddFileResult> AddFile(string fileFullPath, IFileHash fileHash, FileSize fileSize, DateTime dateTime, ConflictResolver? conflictResolver)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ namespace YaR.Clouds.Base.Repos.YandexDisk.YadWeb.Requests
{
class YadAuthAskV2Request : BaseRequestJson<YadAuthAskV2RequestResult>
{
private readonly IAuth _auth;
private readonly string _csrf;
private readonly string _uid;

public YadAuthAskV2Request(HttpCommonSettings settings, IAuth auth, string csrf, string uid)
: base(settings, auth)
{
_auth = auth;
_csrf = csrf;
_uid = uid;
}
Expand Down
156 changes: 70 additions & 86 deletions MailRuCloud/MailRuCloudApi/Cloud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,13 @@ private void FillWithULinks(Folder folder)
folder.Folders.Add(new Folder(0, linkpath) { CreationTimeUtc = flink.CreationDate ?? DateTime.MinValue });
else
{
if (folder.Files.All(inf => inf.FullPath != linkpath))
{
var newfile = new File(linkpath, flink.Size);
{
newfile.PublicLinks.Add(new PublicLinkInfo(flink.Href));
}

if (flink.CreationDate != null)
newfile.LastWriteTimeUtc = flink.CreationDate.Value;
folder.Files.Add(newfile);
}
if (folder.Files.Any(inf => inf.FullPath == linkpath))
continue;

var newfile = new File(linkpath, flink.Size, new PublicLinkInfo(flink.Href));
if (flink.CreationDate != null)
newfile.LastWriteTimeUtc = flink.CreationDate.Value;
folder.Files.Add(newfile);
}
}
}
Expand Down Expand Up @@ -307,12 +303,12 @@ public async Task<PublishInfo> Publish(Folder folder, bool makeShareFile = true)
folder.PublicLinks.Add(new PublicLinkInfo(url));
var info = folder.ToPublishInfo();

if (makeShareFile)
{
string path = WebDavPath.Combine(folder.FullPath, PublishInfo.SharedFilePostfix);
UploadFileJson(path, info)
.ThrowIf(r => !r, _ => new Exception($"Cannot upload JSON file, path = {path}"));
}
if (!makeShareFile)
return info;

string path = WebDavPath.Combine(folder.FullPath, PublishInfo.SharedFilePostfix);
UploadFileJson(path, info)
.ThrowIf(r => !r, _ => new Exception($"Cannot upload JSON file, path = {path}"));

return info;
}
Expand Down Expand Up @@ -347,12 +343,10 @@ public async Task<bool> Copy(Folder folder, string destinationPath)
if (link != null)
{
var cloneres = await CloneItem(destinationPath, link.Href.OriginalString);
if (cloneres.IsSuccess && WebDavPath.Name(cloneres.Path) != link.Name)
{
var renRes = await Rename(cloneres.Path, link.Name);
return renRes;
}
return cloneres.IsSuccess;
if (!cloneres.IsSuccess || WebDavPath.Name(cloneres.Path) == link.Name)
return cloneres.IsSuccess;
var renRes = await Rename(cloneres.Path, link.Name);
return renRes;
}

//var copyRes = await new CopyRequest(CloudApi, folder.FullPath, destinationPath).MakeRequestAsync();
Expand All @@ -365,15 +359,14 @@ public async Task<bool> Copy(Folder folder, string destinationPath)
{
var linkdest = WebDavPath.ModifyParent(linka.MapPath, WebDavPath.Parent(folder.FullPath), destinationPath);
var cloneres = await CloneItem(linkdest, linka.Href.OriginalString);
if (cloneres.IsSuccess && WebDavPath.Name(cloneres.Path) != linka.Name)
{
var renRes = await Rename(cloneres.Path, linka.Name);
if (!renRes)
{
_itemCache.Invalidate(destinationPath);
return false;
}
}
if (!cloneres.IsSuccess || WebDavPath.Name(cloneres.Path) == linka.Name)
continue;

if (await Rename(cloneres.Path, linka.Name))
continue;

_itemCache.Invalidate(destinationPath);
return false;
}

_itemCache.Invalidate(destinationPath);
Expand Down Expand Up @@ -451,13 +444,11 @@ public async Task<bool> Copy(File file, string destinationPath, string newname)
var copyRes = await Account.RequestRepo.Copy(pfile.FullPath, destPath, ConflictResolver.Rewrite);
if (!copyRes.IsSuccess) return false;
if (doRename || WebDavPath.Name(copyRes.NewName) != newname)
{
string newFullPath = WebDavPath.Combine(destPath, WebDavPath.Name(copyRes.NewName));
var renameRes = await Rename(newFullPath, pfile.Name.Replace(file.Name, newname));
if (!renameRes) return false;
}
return true;
if (!doRename && WebDavPath.Name(copyRes.NewName) == newname)
return true;
string newFullPath = WebDavPath.Combine(destPath, WebDavPath.Name(copyRes.NewName));
return await Rename(newFullPath, pfile.Name.Replace(file.Name, newname));
});

_itemCache.Invalidate(destinationPath);
Expand Down Expand Up @@ -509,13 +500,13 @@ public async Task<bool> Rename(File file, string newFileName)
{
var result = await Rename(file.FullPath, newFileName).ConfigureAwait(false);

if (file.Files.Count > 1)
if (file.Files.Count <= 1)
return result;

foreach (var splitFile in file.Parts)
{
foreach (var splitFile in file.Parts)
{
string newSplitName = newFileName + splitFile.ServiceInfo.ToString(false);
await Rename(splitFile.FullPath, newSplitName).ConfigureAwait(false);
}
string newSplitName = newFileName + splitFile.ServiceInfo.ToString(false);
await Rename(splitFile.FullPath, newSplitName).ConfigureAwait(false);
}

return result;
Expand All @@ -536,11 +527,11 @@ private async Task<bool> Rename(string fullPath, string newName)
{
var data = await Account.RequestRepo.Rename(fullPath, newName);

if (data.IsSuccess)
{
LinkManager.ProcessRename(fullPath, newName);
_itemCache.Invalidate(fullPath, WebDavPath.Parent(fullPath));
}
if (!data.IsSuccess)
return data.IsSuccess;

LinkManager.ProcessRename(fullPath, newName);
_itemCache.Invalidate(fullPath, WebDavPath.Parent(fullPath));

return data.IsSuccess;
}
Expand Down Expand Up @@ -621,14 +612,14 @@ public async Task<bool> MoveAsync(Folder folder, string destinationPath)

var linkdest = WebDavPath.ModifyParent(linka.MapPath, WebDavPath.Parent(folder.FullPath), destinationPath);
var cloneres = await CloneItem(linkdest, linka.Href.OriginalString);
if (cloneres.IsSuccess )
if (!cloneres.IsSuccess)
continue;

_itemCache.Invalidate(destinationPath);
if (WebDavPath.Name(cloneres.Path) != linka.Name)
{
_itemCache.Invalidate(destinationPath);
if (WebDavPath.Name(cloneres.Path) != linka.Name)
{
var renRes = await Rename(cloneres.Path, linka.Name);
if (!renRes) return false;
}
var renRes = await Rename(cloneres.Path, linka.Name);
if (!renRes) return false;
}
}
if (links.Any()) LinkManager.Save();
Expand Down Expand Up @@ -749,9 +740,6 @@ public virtual async Task<bool> Remove(File file, bool removeShareDescription =

}

//if (doInvalidateCache)
// _itemCache.Invalidate(file.Path, file.FullPath);

return res;
}

Expand All @@ -776,14 +764,14 @@ private async Task<bool> Remove(string fullPath)
}

var res = await Account.RequestRepo.Remove(fullPath);
if (res.IsSuccess)
{
//remove inner links
var innerLinks = LinkManager.GetChilds(fullPath);
LinkManager.RemoveLinks(innerLinks);
if (!res.IsSuccess)
return res.IsSuccess;

//remove inner links
var innerLinks = LinkManager.GetChilds(fullPath);
LinkManager.RemoveLinks(innerLinks);

_itemCache.Forget(WebDavPath.Parent(fullPath), fullPath); //_itemCache.Invalidate(WebDavPath.Parent(fullPath));
}
_itemCache.Forget(WebDavPath.Parent(fullPath), fullPath); //_itemCache.Invalidate(WebDavPath.Parent(fullPath));
return res.IsSuccess;
}

Expand Down Expand Up @@ -904,23 +892,21 @@ private void OnFileUploaded(IEnumerable<File> files)

public T DownloadFileAsJson<T>(File file)
{
using (var stream = Account.RequestRepo.GetDownloadStream(file)) //new DownloadStream(file, CloudApi))
using (var reader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(reader))
{
var ser = new JsonSerializer();
return ser.Deserialize<T>(jsonReader);
}
using var stream = Account.RequestRepo.GetDownloadStream(file);
using var reader = new StreamReader(stream);
using var jsonReader = new JsonTextReader(reader);

var ser = new JsonSerializer();
return ser.Deserialize<T>(jsonReader);
}

public string DownloadFileAsString(File file)
{
using (var stream = Account.RequestRepo.GetDownloadStream(file)) //new DownloadStream(file, CloudApi))
using (var reader = new StreamReader(stream))
{
string res = reader.ReadToEnd();
return res;
}
using var stream = Account.RequestRepo.GetDownloadStream(file);
using var reader = new StreamReader(stream);

string res = reader.ReadToEnd();
return res;
}

/// <summary>
Expand Down Expand Up @@ -978,14 +964,12 @@ public bool UploadFileJson<T>(string fullFilePath, T data, bool discardEncryptio

protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
if (_disposedValue) return;
if (disposing)
{
if (disposing)
{
CancelToken.Dispose();
}
_disposedValue = true;
CancelToken.Dispose();
}
_disposedValue = true;
}

public void Dispose()
Expand Down

0 comments on commit a4da3a6

Please sign in to comment.