Files: thirdparty refactoring

This commit is contained in:
pavelbannov 2020-03-17 10:48:33 +03:00
parent 8f455e388f
commit ff36878397
23 changed files with 88 additions and 161 deletions

View File

@ -41,5 +41,6 @@ namespace ASC.Files.Core
bool CheckAccess(); bool CheckAccess();
void InvalidateStorage(); void InvalidateStorage();
void UpdateTitle(string newtitle);
} }
} }

View File

@ -86,7 +86,10 @@ namespace ASC.Files.Thirdparty.Box
public void Dispose() public void Dispose()
{ {
BoxProviderInfo.Dispose(); if (BoxProviderInfo != null)
{
BoxProviderInfo.Dispose();
}
} }
protected string MappingID(string id, bool saveIfNotExist = false) protected string MappingID(string id, bool saveIfNotExist = false)

View File

@ -30,8 +30,6 @@ using ASC.Common;
using ASC.Files.Core; using ASC.Files.Core;
using ASC.Files.Core.Security; using ASC.Files.Core.Security;
using Microsoft.Extensions.DependencyInjection;
namespace ASC.Files.Thirdparty.Box namespace ASC.Files.Thirdparty.Box
{ {
internal class BoxDaoSelector : RegexDaoSelectorBase<BoxProviderInfo>, IDaoSelector internal class BoxDaoSelector : RegexDaoSelectorBase<BoxProviderInfo>, IDaoSelector
@ -63,27 +61,8 @@ namespace ASC.Files.Thirdparty.Box
{ {
return base.GetSecurityDao<BoxSecurityDao>(id); return base.GetSecurityDao<BoxSecurityDao>(id);
} }
public override string GetIdCode(string id)
{
if (id != null)
{
var match = Selector.Match(id);
if (match.Success)
{
return match.Groups["id"].Value;
}
}
return base.GetIdCode(id);
}
public override void RenameProvider(BoxProviderInfo boxProviderInfo, string newTitle)
{
var dbDao = ServiceProvider.GetService<CachedProviderAccountDao>();
dbDao.UpdateProviderInfo(boxProviderInfo.ID, newTitle, null, boxProviderInfo.RootFolderType);
boxProviderInfo.UpdateTitle(newTitle); //This will update cached version too
}
} }
public static class BoxDaoSelectorExtention public static class BoxDaoSelectorExtention
{ {
public static DIHelper AddBoxDaoSelectorService(this DIHelper services) public static DIHelper AddBoxDaoSelectorService(this DIHelper services)

View File

@ -135,7 +135,7 @@ namespace ASC.Files.Thirdparty.Box
CacheReset(); CacheReset();
} }
internal void UpdateTitle(string newtitle) public void UpdateTitle(string newtitle)
{ {
CustomerTitle = newtitle; CustomerTitle = newtitle;
} }

View File

@ -86,7 +86,10 @@ namespace ASC.Files.Thirdparty.Dropbox
public void Dispose() public void Dispose()
{ {
DropboxProviderInfo.Dispose(); if (DropboxProviderInfo != null)
{
DropboxProviderInfo.Dispose();
}
} }
protected string MappingID(string id, bool saveIfNotExist = false) protected string MappingID(string id, bool saveIfNotExist = false)

View File

@ -25,14 +25,11 @@
using System; using System;
using System.Globalization;
using ASC.Common; using ASC.Common;
using ASC.Files.Core; using ASC.Files.Core;
using ASC.Files.Core.Security; using ASC.Files.Core.Security;
using Microsoft.Extensions.DependencyInjection;
namespace ASC.Files.Thirdparty.Dropbox namespace ASC.Files.Thirdparty.Dropbox
{ {
internal class DropboxDaoSelector : RegexDaoSelectorBase<DropboxProviderInfo>, IDaoSelector internal class DropboxDaoSelector : RegexDaoSelectorBase<DropboxProviderInfo>, IDaoSelector
@ -64,27 +61,6 @@ namespace ASC.Files.Thirdparty.Dropbox
{ {
return base.GetSecurityDao<DropboxSecurityDao>(id); return base.GetSecurityDao<DropboxSecurityDao>(id);
} }
public override string GetIdCode(string id)
{
if (id != null)
{
var match = Selector.Match(Convert.ToString(id, CultureInfo.InvariantCulture));
if (match.Success)
{
return match.Groups["id"].Value;
}
throw new ArgumentException("Id is not a Dropbox id");
}
return base.GetIdCode(id);
}
public override void RenameProvider(DropboxProviderInfo dropboxProviderInfo, string newTitle)
{
var dbDao = ServiceProvider.GetService<CachedProviderAccountDao>();
dbDao.UpdateProviderInfo(dropboxProviderInfo.ID, newTitle, null, dropboxProviderInfo.RootFolderType);
dropboxProviderInfo.UpdateTitle(newTitle); //This will update cached version too
}
} }
public static class DropboxDaoSelectorExtention public static class DropboxDaoSelectorExtention

View File

@ -118,7 +118,7 @@ namespace ASC.Files.Thirdparty.Dropbox
CacheReset(); CacheReset();
} }
internal void UpdateTitle(string newtitle) public void UpdateTitle(string newtitle)
{ {
CustomerTitle = newtitle; CustomerTitle = newtitle;
} }

View File

@ -92,7 +92,10 @@ namespace ASC.Files.Thirdparty.GoogleDrive
public void Dispose() public void Dispose()
{ {
GoogleDriveProviderInfo.Dispose(); if (GoogleDriveProviderInfo != null)
{
GoogleDriveProviderInfo.Dispose();
}
} }
protected string MappingID(string id, bool saveIfNotExist = false) protected string MappingID(string id, bool saveIfNotExist = false)

View File

@ -30,8 +30,6 @@ using ASC.Common;
using ASC.Files.Core; using ASC.Files.Core;
using ASC.Files.Core.Security; using ASC.Files.Core.Security;
using Microsoft.Extensions.DependencyInjection;
namespace ASC.Files.Thirdparty.GoogleDrive namespace ASC.Files.Thirdparty.GoogleDrive
{ {
internal class GoogleDriveDaoSelector : RegexDaoSelectorBase<GoogleDriveProviderInfo>, IDaoSelector internal class GoogleDriveDaoSelector : RegexDaoSelectorBase<GoogleDriveProviderInfo>, IDaoSelector
@ -62,26 +60,6 @@ namespace ASC.Files.Thirdparty.GoogleDrive
{ {
return base.GetSecurityDao<GoogleDriveSecurityDao>(id); return base.GetSecurityDao<GoogleDriveSecurityDao>(id);
} }
public override string GetIdCode(string id)
{
if (id != null)
{
var match = Selector.Match(id);
if (match.Success)
{
return match.Groups["id"].Value;
}
}
return base.GetIdCode(id);
}
public override void RenameProvider(GoogleDriveProviderInfo googleDriveProviderInfo, string newTitle)
{
var dbDao = ServiceProvider.GetService<CachedProviderAccountDao>();
dbDao.UpdateProviderInfo(googleDriveProviderInfo.ID, newTitle, null, googleDriveProviderInfo.RootFolderType);
googleDriveProviderInfo.UpdateTitle(newTitle); //This will update cached version too
}
} }
public static class GoogleDriveDaoSelectorExtention public static class GoogleDriveDaoSelectorExtention

View File

@ -148,7 +148,7 @@ namespace ASC.Files.Thirdparty.GoogleDrive
CacheReset(); CacheReset();
} }
internal void UpdateTitle(string newtitle) public void UpdateTitle(string newtitle)
{ {
CustomerTitle = newtitle; CustomerTitle = newtitle;
} }

View File

@ -24,12 +24,14 @@
*/ */
using System;
using ASC.Files.Core; using ASC.Files.Core;
using ASC.Files.Core.Security; using ASC.Files.Core.Security;
namespace ASC.Files.Thirdparty namespace ASC.Files.Thirdparty
{ {
internal interface IDaoSelector internal interface IDaoSelector : IDisposable
{ {
bool IsMatch(string id); bool IsMatch(string id);
IFileDao<string> GetFileDao(string id); IFileDao<string> GetFileDao(string id);

View File

@ -0,0 +1,12 @@
using System;
using ASC.Files.Core;
using ASC.Files.Core.Thirdparty;
namespace ASC.Files.Thirdparty
{
internal interface IThirdPartyProviderDao<T> : IDisposable where T : class, IProviderInfo
{
void Init(BaseProviderInfo<T> t1, RegexDaoSelectorBase<T> selectorBase);
}
}

View File

@ -84,7 +84,10 @@ namespace ASC.Files.Thirdparty.OneDrive
public void Dispose() public void Dispose()
{ {
OneDriveProviderInfo.Dispose(); if (OneDriveProviderInfo != null)
{
OneDriveProviderInfo.Dispose();
}
} }
protected string MappingID(string id, bool saveIfNotExist = false) protected string MappingID(string id, bool saveIfNotExist = false)

View File

@ -30,8 +30,6 @@ using ASC.Common;
using ASC.Files.Core; using ASC.Files.Core;
using ASC.Files.Core.Security; using ASC.Files.Core.Security;
using Microsoft.Extensions.DependencyInjection;
namespace ASC.Files.Thirdparty.OneDrive namespace ASC.Files.Thirdparty.OneDrive
{ {
internal class OneDriveDaoSelector : RegexDaoSelectorBase<OneDriveProviderInfo>, IDaoSelector internal class OneDriveDaoSelector : RegexDaoSelectorBase<OneDriveProviderInfo>, IDaoSelector
@ -63,27 +61,6 @@ namespace ASC.Files.Thirdparty.OneDrive
{ {
return base.GetSecurityDao<OneDriveSecurityDao>(id); return base.GetSecurityDao<OneDriveSecurityDao>(id);
} }
public override string GetIdCode(string id)
{
if (id != null)
{
var match = Selector.Match(id);
if (match.Success)
{
return match.Groups["id"].Value;
}
}
return base.GetIdCode(id);
}
public override void RenameProvider(OneDriveProviderInfo onedriveProviderInfo, string newTitle)
{
var dbDao = ServiceProvider.GetService<CachedProviderAccountDao>();
dbDao.UpdateProviderInfo(onedriveProviderInfo.ID, newTitle, null, onedriveProviderInfo.RootFolderType);
onedriveProviderInfo.UpdateTitle(newTitle); //This will update cached version too
}
} }
public static class OneDriveDaoSelectorExtention public static class OneDriveDaoSelectorExtention

View File

@ -119,10 +119,11 @@ namespace ASC.Files.Thirdparty.OneDrive
CacheReset(); CacheReset();
} }
internal void UpdateTitle(string newtitle) public void UpdateTitle(string newtitle)
{ {
CustomerTitle = newtitle; CustomerTitle = newtitle;
} }
internal Item GetOneDriveItem(string itemId) internal Item GetOneDriveItem(string itemId)
{ {
return OneDriveProviderInfoHelper.GetOneDriveItem(Storage, ID, itemId); return OneDriveProviderInfoHelper.GetOneDriveItem(Storage, ID, itemId);

View File

@ -263,8 +263,7 @@ namespace ASC.Files.Thirdparty.ProviderDao
public void Dispose() public void Dispose()
{ {
//TODO Selectors.ForEach(r => r.Dispose());
//throw new NotImplementedException();
} }
} }

View File

@ -25,6 +25,7 @@
using System; using System;
using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using ASC.Files.Core; using ASC.Files.Core;
@ -35,7 +36,7 @@ using Microsoft.Extensions.DependencyInjection;
namespace ASC.Files.Thirdparty namespace ASC.Files.Thirdparty
{ {
internal abstract class RegexDaoSelectorBase<T> : IDaoSelector<T>, IDisposable where T : class, IProviderInfo internal abstract class RegexDaoSelectorBase<T> : IDaoSelector<T> where T : class, IProviderInfo
{ {
public IServiceProvider ServiceProvider { get; } public IServiceProvider ServiceProvider { get; }
public IDaoFactory DaoFactory { get; } public IDaoFactory DaoFactory { get; }
@ -43,6 +44,8 @@ namespace ASC.Files.Thirdparty
protected internal abstract string Name { get; } protected internal abstract string Name { get; }
protected internal abstract string Id { get; } protected internal abstract string Id { get; }
private Dictionary<string, IThirdPartyProviderDao<T>> Providers { get; set; }
protected RegexDaoSelectorBase( protected RegexDaoSelectorBase(
IServiceProvider serviceProvider, IServiceProvider serviceProvider,
IDaoFactory daoFactory) IDaoFactory daoFactory)
@ -50,6 +53,7 @@ namespace ASC.Files.Thirdparty
ServiceProvider = serviceProvider; ServiceProvider = serviceProvider;
DaoFactory = daoFactory; DaoFactory = daoFactory;
Selector = new Regex(@"^" + Id + @"-(?'id'\d+)(-(?'path'.*)){0,1}$", RegexOptions.Singleline | RegexOptions.Compiled); Selector = new Regex(@"^" + Id + @"-(?'id'\d+)(-(?'path'.*)){0,1}$", RegexOptions.Singleline | RegexOptions.Compiled);
Providers = new Dictionary<string, IThirdPartyProviderDao<T>>();
} }
public virtual string ConvertId(string id) public virtual string ConvertId(string id)
@ -71,9 +75,17 @@ namespace ASC.Files.Thirdparty
} }
} }
public virtual string GetIdCode(string id) public string GetIdCode(string id)
{ {
return null; if (id != null)
{
var match = Selector.Match(id);
if (match.Success)
{
return match.Groups["id"].Value;
}
}
throw new ArgumentException($"Id is not a {Name} id");
} }
public virtual bool IsMatch(string id) public virtual bool IsMatch(string id)
@ -103,10 +115,14 @@ namespace ASC.Files.Thirdparty
private T1 GetDao<T1>(string id) where T1 : IThirdPartyProviderDao<T> private T1 GetDao<T1>(string id) where T1 : IThirdPartyProviderDao<T>
{ {
if (Providers.ContainsKey(id)) return (T1)Providers[id];
var res = ServiceProvider.GetService<T1>(); var res = ServiceProvider.GetService<T1>();
res.Init(GetInfo(id), this); res.Init(GetInfo(id), this);
Providers.Add(id, res);
return res; return res;
} }
@ -130,7 +146,12 @@ namespace ASC.Files.Thirdparty
throw new ArgumentException($"Id is not {Name} id"); throw new ArgumentException($"Id is not {Name} id");
} }
public abstract void RenameProvider(T provider, string newTitle); public void RenameProvider(T provider, string newTitle)
{
var dbDao = ServiceProvider.GetService<CachedProviderAccountDao>();
dbDao.UpdateProviderInfo(provider.ID, newTitle, null, provider.RootFolderType);
provider.UpdateTitle(newTitle); //This will update cached version too
}
protected virtual T GetProviderInfo(int linkId) protected virtual T GetProviderInfo(int linkId)
{ {
@ -147,13 +168,10 @@ namespace ASC.Files.Thirdparty
public void Dispose() public void Dispose()
{ {
//TODO foreach (var p in Providers)
//throw new NotImplementedException(); {
p.Value.Dispose();
}
} }
} }
internal interface IThirdPartyProviderDao<T> where T : class, IProviderInfo
{
void Init(BaseProviderInfo<T> t1, RegexDaoSelectorBase<T> selectorBase);
}
} }

View File

@ -207,5 +207,13 @@ namespace ASC.Files.Thirdparty.SharePoint
{ {
return MappingID(id, false); return MappingID(id, false);
} }
public void Dispose()
{
if (ProviderInfo != null)
{
ProviderInfo.Dispose();
}
}
} }
} }

View File

@ -30,8 +30,6 @@ using ASC.Common;
using ASC.Files.Core; using ASC.Files.Core;
using ASC.Files.Core.Security; using ASC.Files.Core.Security;
using Microsoft.Extensions.DependencyInjection;
namespace ASC.Files.Thirdparty.SharePoint namespace ASC.Files.Thirdparty.SharePoint
{ {
internal class SharePointDaoSelector : RegexDaoSelectorBase<SharePointProviderInfo>, IDaoSelector internal class SharePointDaoSelector : RegexDaoSelectorBase<SharePointProviderInfo>, IDaoSelector
@ -76,26 +74,6 @@ namespace ASC.Files.Thirdparty.SharePoint
} }
return base.ConvertId(null); return base.ConvertId(null);
} }
public override string GetIdCode(string id)
{
if (id != null)
{
var match = Selector.Match(id);
if (match.Success)
{
return match.Groups["id"].Value;
}
}
return base.GetIdCode(id);
}
public override void RenameProvider(SharePointProviderInfo sharePointProviderInfo, string newTitle)
{
var dbDao = ServiceProvider.GetService<CachedProviderAccountDao>();
dbDao.UpdateProviderInfo(sharePointProviderInfo.ID, newTitle, null, sharePointProviderInfo.RootFolderType);
sharePointProviderInfo.UpdateTitle(newTitle); //This will update cached version too
}
} }
public static class SharePointDaoSelectorExtention public static class SharePointDaoSelectorExtention

View File

@ -95,7 +95,7 @@ namespace ASC.Files.Thirdparty.SharePoint
SharePointProviderInfoHelper.Invalidate(); SharePointProviderInfoHelper.Invalidate();
} }
internal void UpdateTitle(string newtitle) public void UpdateTitle(string newtitle)
{ {
CustomerTitle = newtitle; CustomerTitle = newtitle;
} }

View File

@ -571,5 +571,13 @@ namespace ASC.Files.Thirdparty.Sharpbox
var staticText = match.Value.Substring(string.Format(" ({0})", index).Length); var staticText = match.Value.Substring(string.Format(" ({0})", index).Length);
return string.Format(" ({0}){1}", index + 1, staticText); return string.Format(" ({0}){1}", index + 1, staticText);
} }
public void Dispose()
{
if (SharpBoxProviderInfo != null)
{
SharpBoxProviderInfo.Dispose();
}
}
} }
} }

View File

@ -30,8 +30,6 @@ using ASC.Common;
using ASC.Files.Core; using ASC.Files.Core;
using ASC.Files.Core.Security; using ASC.Files.Core.Security;
using Microsoft.Extensions.DependencyInjection;
namespace ASC.Files.Thirdparty.Sharpbox namespace ASC.Files.Thirdparty.Sharpbox
{ {
internal class SharpBoxDaoSelector : RegexDaoSelectorBase<SharpBoxProviderInfo>, IDaoSelector internal class SharpBoxDaoSelector : RegexDaoSelectorBase<SharpBoxProviderInfo>, IDaoSelector
@ -63,26 +61,6 @@ namespace ASC.Files.Thirdparty.Sharpbox
{ {
return base.GetSecurityDao<SharpBoxSecurityDao>(id); return base.GetSecurityDao<SharpBoxSecurityDao>(id);
} }
public override string GetIdCode(string id)
{
if (id != null)
{
var match = Selector.Match(id);
if (match.Success)
{
return match.Groups["id"].Value;
}
}
return base.GetIdCode(id);
}
public override void RenameProvider(SharpBoxProviderInfo sharpBoxProviderInfo, string newTitle)
{
var dbDao = ServiceProvider.GetService<CachedProviderAccountDao>();
dbDao.UpdateProviderInfo(sharpBoxProviderInfo.ID, newTitle, null, sharpBoxProviderInfo.RootFolderType);
sharpBoxProviderInfo.UpdateTitle(newTitle); //This will update cached version too
}
} }
public static class SharpBoxDaoSelectorExtention public static class SharpBoxDaoSelectorExtention

View File

@ -79,7 +79,7 @@ namespace ASC.Files.Thirdparty.Sharpbox
get => Wrapper.Storage != null && Wrapper.Storage.IsOpened; get => Wrapper.Storage != null && Wrapper.Storage.IsOpened;
} }
internal void UpdateTitle(string newtitle) public void UpdateTitle(string newtitle)
{ {
CustomerTitle = newtitle; CustomerTitle = newtitle;
} }