Skip to content

Commit

Permalink
ASC.Files: moved from 08a01422
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbannov committed May 26, 2021
1 parent f6fbe33 commit d8adef7
Show file tree
Hide file tree
Showing 128 changed files with 989 additions and 270 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "products/ASC.Files/Server/DocStore"]
path = products/ASC.Files/Server/DocStore
url = https://github.com/ONLYOFFICE/document-templates
branch = main/community-server
1 change: 1 addition & 0 deletions products/ASC.Files/Core/ASC.Files.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<PackageReference Include="Microsoft.SharePoint.Client" Version="14.0.4762.1000" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
<PackageReference Include="SharpCompress" Version="0.26.0" />
<PackageReference Include="SharpZipLib" Version="1.3.2" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
</ItemGroup>

Expand Down
16 changes: 15 additions & 1 deletion products/ASC.Files/Core/Configuration/FilesSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public class FilesSettings : ISettings
[JsonPropertyName("HideTemplates")]
public bool HideTemplatesSetting { get; set; }

[JsonPropertyName("DownloadZip")]
public bool DownloadTarGzSetting { get; set; }

public ISettings GetDefault(IServiceProvider serviceProvider)
{
Expand All @@ -97,7 +99,8 @@ public ISettings GetDefault(IServiceProvider serviceProvider)
StoreForcesaveSetting = false,
HideRecentSetting = false,
HideFavoritesSetting = false,
HideTemplatesSetting = false
HideTemplatesSetting = false,
DownloadTarGzSetting = false
};
}

Expand Down Expand Up @@ -268,6 +271,17 @@ public bool TemplatesSection
get { return !LoadForCurrentUser().HideTemplatesSetting; }
}

public bool DownloadTarGz
{
set
{
var setting = LoadForCurrentUser();
setting.DownloadTarGzSetting = value;
SaveForCurrentUser(setting);
}
get => LoadForCurrentUser().DownloadTarGzSetting;
}

private FilesSettings LoadForCurrentUser()
{
return SettingsManager.LoadForCurrentUser<FilesSettings>();
Expand Down
93 changes: 93 additions & 0 deletions products/ASC.Files/Core/Core/Compress/CompressToArchive.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
*
* (c) Copyright Ascensio System Limited 2010-2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

using System;
using System.Collections.Generic;
using System.IO;

using ASC.Common;
using ASC.Web.Files.Classes;

using Microsoft.Extensions.DependencyInjection;

namespace ASC.Web.Files.Core.Compress
{
/// <summary>
/// Archives the data stream in the format selected in the settings
/// </summary>
[Scope]
public class CompressToArchive : ICompress
{
private readonly ICompress compress;

internal static string TarExt = ".tar.gz";
internal static string ZipExt = ".zip";
private static List<string> Exts = new List<string>(2) { TarExt, ZipExt };

public CompressToArchive(FilesSettingsHelper filesSettings, CompressToTarGz compressToTarGz, CompressToZip compressToZip)
{
compress = filesSettings.DownloadTarGz
? compressToTarGz
: compressToZip;
}

public static string GetExt(IServiceProvider serviceProvider, string ext)
{
if (Exts.Contains(ext)) return ext;
using var zip = serviceProvider.GetService<CompressToArchive>();
return zip.ArchiveExtension;
}

public void SetStream(Stream stream) => compress.SetStream(stream);

/// <summary>
/// The record name is created (the name of a separate file in the archive)
/// </summary>
/// <param name="title">File name with extension, this name will have the file in the archive</param>
public void CreateEntry(string title) => compress.CreateEntry(title);

/// <summary>
/// Transfer the file itself to the archive
/// </summary>
/// <param name="readStream">File data</param>
public void PutStream(Stream readStream) => compress.PutStream(readStream);

/// <summary>
/// Put an entry on the output stream.
/// </summary>
public void PutNextEntry() => compress.PutNextEntry();

/// <summary>
/// Closes the current entry.
/// </summary>
public void CloseEntry() => compress.CloseEntry();

/// <summary>
/// Resource title (does not affect the work of the class)
/// </summary>
/// <returns></returns>
public string Title => compress.Title;

/// <summary>
/// Extension the archive (does not affect the work of the class)
/// </summary>
/// <returns></returns>
public string ArchiveExtension => compress.ArchiveExtension;

/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose() => compress.Dispose();
}
}
109 changes: 109 additions & 0 deletions products/ASC.Files/Core/Core/Compress/CompressToTarGz.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
*
* (c) Copyright Ascensio System Limited 2010-2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

using System.IO;
using System.Text;

using ASC.Common;

using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;

namespace ASC.Web.Files.Core.Compress
{
/// <summary>
/// Archives the data stream into the format .tar.gz
/// </summary>
public class CompressToTarGz : ICompress
{
private GZipOutputStream gzoStream;
private TarOutputStream gzip;
private TarEntry tarEntry;
private TempStream TempStream { get; }

internal CompressToTarGz(TempStream tempStream)
{
TempStream = tempStream;
}

/// <summary></summary>
/// <param name="stream">Accepts a new stream, it will contain an archive upon completion of work</param>
public void SetStream(Stream stream)
{
gzoStream = new GZipOutputStream(stream) { IsStreamOwner = false };
gzip = new TarOutputStream(gzoStream, Encoding.UTF8);
gzoStream.IsStreamOwner = false;
}

/// <summary>
/// The record name is created (the name of a separate file in the archive)
/// </summary>
/// <param name="title">File name with extension, this name will have the file in the archive</param>
public void CreateEntry(string title)
{
tarEntry = TarEntry.CreateTarEntry(title);
}

/// <summary>
/// Transfer the file itself to the archive
/// </summary>
/// <param name="readStream">File data</param>
public void PutStream(Stream readStream)
{
using (var buffered = TempStream.GetBuffered(readStream))
{
tarEntry.Size = buffered.Length;
gzip.PutNextEntry(tarEntry);
buffered.CopyTo(gzip);
}
}

/// <summary>
/// Put an entry on the output stream.
/// </summary>
public void PutNextEntry()
{
gzip.PutNextEntry(tarEntry);
}

/// <summary>
/// Closes the current entry.
/// </summary>
public void CloseEntry()
{
gzip.CloseEntry();
}

/// <summary>
/// Resource title (does not affect the work of the class)
/// </summary>
/// <returns></returns>
public string Title => FilesUCResource.FilesWillBeCompressedTarGz;

/// <summary>
/// Extension the archive (does not affect the work of the class)
/// </summary>
/// <returns></returns>
public string ArchiveExtension => CompressToArchive.TarExt;

/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
gzip?.Dispose();
gzoStream?.Dispose();
}
}
}
103 changes: 103 additions & 0 deletions products/ASC.Files/Core/Core/Compress/CompressToZip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
*
* (c) Copyright Ascensio System Limited 2010-2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

using System.IO;

using ASC.Common;

using ICSharpCode.SharpZipLib.Zip;

namespace ASC.Web.Files.Core.Compress
{
/// <summary>
/// Archives the data stream into the format .zip
/// </summary>
public class CompressToZip : ICompress
{
private ZipOutputStream zipStream;
private ZipEntry zipEntry;
private TempStream TempStream { get; }

internal CompressToZip(TempStream tempStream)
{
TempStream = tempStream;
}

/// <summary> </summary>
/// <param name="stream">Accepts a new stream, it will contain an archive upon completion of work</param>
public void SetStream(Stream stream)
{
zipStream = new ZipOutputStream(stream) { UseZip64 = UseZip64.Dynamic };
zipStream.IsStreamOwner = false;
}

/// <summary>
/// The record name is created (the name of a separate file in the archive)
/// </summary>
/// <param name="title">File name with extension, this name will have the file in the archive</param>
public void CreateEntry(string title)
{
zipEntry = new ZipEntry(title);
}

/// <summary>
/// Transfer the file itself to the archive
/// </summary>
/// <param name="readStream">File data</param>
public void PutStream(Stream readStream)
{
using (var buffered = TempStream.GetBuffered(readStream))
{
zipEntry.Size = buffered.Length;
zipStream.PutNextEntry(zipEntry);
buffered.CopyTo(zipStream);
}
}

/// <summary>
/// Put an entry on the output stream.
/// </summary>
public void PutNextEntry()
{
zipStream.PutNextEntry(zipEntry);
}

/// <summary>
/// Closes the current entry.
/// </summary>
public void CloseEntry()
{
zipStream.CloseEntry();
}

/// <summary>
/// Resource title (does not affect the work of the class)
/// </summary>
/// <returns></returns>
public string Title => FilesUCResource.FilesWillBeCompressedZip;

/// <summary>
/// Extension the archive (does not affect the work of the class)
/// </summary>
/// <returns></returns>
public string ArchiveExtension => CompressToArchive.ZipExt;

public void Dispose()
{
zipStream?.Dispose();
}
}
}
Loading

0 comments on commit d8adef7

Please sign in to comment.