Skip to content

Commit

Permalink
Merge pull request #272 from ONLYOFFICE/feature/remove_binary_formatter
Browse files Browse the repository at this point in the history
Feature/remove binary formatter
  • Loading branch information
pavelbannov authored Jun 23, 2021
2 parents d59f9a2 + 8998a72 commit 1e5d4da
Show file tree
Hide file tree
Showing 20 changed files with 342 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace ASC.Core.ChunkedUploader
{
Expand All @@ -54,23 +56,29 @@ public class CommonChunkedUploadSession : ICloneable

public string CultureName { get; set; }

public Dictionary<string, object> Items = new Dictionary<string, object>();
public Dictionary<string, object> Items { get; set; } = new Dictionary<string, object>();

private const string TempPathKey = "TempPath";

[JsonIgnore]
public string TempPath
{
get { return GetItemOrDefault<string>(TempPathKey); }
set { Items[TempPathKey] = value; }
}

private const string UploadIdKey = "UploadId";

[JsonIgnore]
public string UploadId
{
get { return GetItemOrDefault<string>(UploadIdKey); }
set { Items[UploadIdKey] = value; }
}

private const string ChunksBufferKey = "ChunksBuffer";

[JsonIgnore]
public string ChunksBuffer
{
get { return GetItemOrDefault<string>(ChunksBufferKey); }
Expand All @@ -91,16 +99,41 @@ public T GetItemOrDefault<T>(string key)
return Items.ContainsKey(key) && Items[key] is T t ? t : default;
}

public Stream Serialize()
public virtual Stream Serialize()
{
var stream = new MemoryStream();
new BinaryFormatter().Serialize(stream, this);
return stream;
return null;
}

public static CommonChunkedUploadSession Deserialize(Stream stream)
public void TransformItems()
{
return (CommonChunkedUploadSession)new BinaryFormatter().Deserialize(stream);
var newItems = new Dictionary<string, object>();
foreach(var item in Items)
{
if (item.Value != null)
{
if (item.Value.GetType() == typeof(JsonElement))
{
var value = (JsonElement)item.Value;
if (value.ValueKind == JsonValueKind.String)
{
newItems.Add(item.Key, item.Value.ToString());
}
if (value.ValueKind == JsonValueKind.Number)
{
newItems.Add(item.Key, Int32.Parse(item.Value.ToString()));
}
if (value.ValueKind == JsonValueKind.Array)
{
newItems.Add(item.Key, value.EnumerateArray().Select(o => o.ToString()).ToList());
}
}
else
{
newItems.Add(item.Key, item.Value);
}
}
}
Items = newItems;
}

public virtual object Clone()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,11 @@ public void Remove(CommonChunkedUploadSession s)
{
DataStore.Delete(Domain, GetPathWithId(s.Id));
}

public CommonChunkedUploadSession Get(string sessionId)
{
using var stream = DataStore.GetReadStream(Domain, GetPathWithId(sessionId));
return CommonChunkedUploadSession.Deserialize(stream);
}

public Stream GetStream(string sessionId)
{
return DataStore.GetReadStream(Domain, GetPathWithId(sessionId));
}

public void Init(CommonChunkedUploadSession chunkedUploadSession)
{
Expand Down
21 changes: 20 additions & 1 deletion products/ASC.Files/Core/Core/Entries/ChunkedUploadSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

using System.Text;
using System.Text.Json;

using ASC.Common;
using ASC.Common.Logging;
using ASC.Core.ChunkedUploader;
Expand Down Expand Up @@ -62,6 +65,22 @@ public override object Clone()
var clone = (ChunkedUploadSession<T>)MemberwiseClone();
clone.File = (File<T>)File.Clone();
return clone;
}

public override Stream Serialize()
{
var str = JsonSerializer.Serialize(this);
var stream = new MemoryStream(Encoding.UTF8.GetBytes(str));
return stream;
}

public static ChunkedUploadSession<T> Deserialize(Stream stream, FileHelper fileHelper)
{
var chunkedUploadSession = JsonSerializer.DeserializeAsync<ChunkedUploadSession<T>>(stream).Result;
chunkedUploadSession.File.FileHelper = fileHelper;
chunkedUploadSession.TransformItems();
return chunkedUploadSession;

}
}

Expand Down
113 changes: 34 additions & 79 deletions products/ASC.Files/Core/Core/Entries/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@

using ASC.Common;
using ASC.Web.Core.Files;
using ASC.Web.Files.Classes;
using ASC.Web.Files.Utils;
using ASC.Web.Studio.Core;

namespace ASC.Files.Core
Expand Down Expand Up @@ -60,28 +58,23 @@ public enum FileStatus
[Serializable]
[DebuggerDisplay("{Title} ({ID} v{Version})")]
public class File<T> : FileEntry<T>
{
private FileStatus _status;

public File(Global global,
FilesLinkUtility filesLinkUtility,
FileUtility fileUtility,
FileConverter fileConverter,
FileTrackerHelper fileTracker)
: base(global)
{
Version = 1;
VersionGroup = 1;
FileEntryType = FileEntryType.File;
FilesLinkUtility = filesLinkUtility;
FileUtility = fileUtility;
FileConverter = fileConverter;
FileTracker = fileTracker;
{
private FileStatus _status;

public File()
{
Version = 1;
VersionGroup = 1;
FileEntryType = FileEntryType.File;
}

public File(FileHelper fileHelper): this()
{
FileHelper = fileHelper;
}

public int Version { get; set; }

[JsonPropertyName("version_group")]
public int VersionGroup { get; set; }

public string Comment { get; set; }
Expand All @@ -92,27 +85,15 @@ public string PureTitle
set { base.Title = value; }
}

public override string Title
{
get
{
return string.IsNullOrEmpty(ConvertedType)
? base.Title
: FileUtility.ReplaceFileExtension(base.Title, FileUtility.GetInternalExtension(base.Title));
}
set { base.Title = value; }
}

[JsonPropertyName("content_length")]
public long ContentLength { get; set; }

[JsonPropertyName("content_length_string")]

[JsonIgnore]
public string ContentLengthString
{
get { return FileSizeComment.FilesSizeToString(ContentLength); }
set { }
}


[JsonIgnore]
public FilterType FilterType
{
get
Expand All @@ -136,38 +117,26 @@ public FilterType FilterType

return FilterType.None;
}
}

[JsonPropertyName("file_status")]
}

public FileStatus FileStatus
{
get
{
if (FileTracker.IsEditing(ID))
{
_status |= FileStatus.IsEditing;
}

if (FileTracker.IsEditingAlone(ID))
{
_status |= FileStatus.IsEditingAlone;
}

if (FileConverter.IsConverting(this))
{
_status |= FileStatus.IsConverting;
}

return _status;
}
set { _status = value; }
get => FileHelper.GetFileStatus(this, ref _status);
set => _status = value;
}

[JsonIgnore]
public override string Title { get => FileHelper.GetTitle(this); }


[JsonIgnore]
public string DownloadUrl { get => FileHelper.GetDownloadUrl(this); }

public bool Locked { get; set; }

[JsonPropertyName("locked_by")]
public string LockedBy { get; set; }


[JsonIgnore]
public override bool IsNew
{
get { return (_status & FileStatus.IsNew) == FileStatus.IsNew; }
Expand All @@ -180,6 +149,7 @@ public override bool IsNew
}
}

[JsonIgnore]
public bool IsFavorite
{
get { return (_status & FileStatus.IsFavorite) == FileStatus.IsFavorite; }
Expand All @@ -192,6 +162,7 @@ public bool IsFavorite
}
}

[JsonIgnore]
public bool IsTemplate
{
get { return (_status & FileStatus.IsTemplate) == FileStatus.IsTemplate; }
Expand All @@ -206,18 +177,13 @@ public bool IsTemplate

public bool Encrypted { get; set; }

[JsonPropertyName("thumbnail_status")]
public Thumbnail ThumbnailStatus { get; set; }

public ForcesaveType Forcesave { get; set; }

public string DownloadUrl
{
get { return FilesLinkUtility.GetFileDownloadUrl(ID); }
}

public string ConvertedType { get; set; }


[JsonIgnore]
public string ConvertedExtension
{
get
Expand All @@ -236,17 +202,6 @@ public string ConvertedExtension
}

public object NativeAccessor { get; set; }

[NonSerialized]
private FileTrackerHelper FileTracker;

[NonSerialized]
private readonly FilesLinkUtility FilesLinkUtility;

[NonSerialized]
private readonly FileUtility FileUtility;

[NonSerialized]
private readonly FileConverter FileConverter;
}
}
Loading

0 comments on commit 1e5d4da

Please sign in to comment.