2021-08-31 09:40:28 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (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.IO;
|
2022-03-24 17:56:51 +00:00
|
|
|
using System.Text.Json;
|
|
|
|
using System.Threading.Tasks;
|
2021-08-31 09:40:28 +00:00
|
|
|
|
|
|
|
using ASC.Core;
|
2022-03-24 17:56:51 +00:00
|
|
|
using ASC.Web.Core.Utility;
|
|
|
|
using ASC.Web.Studio.Core;
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-03-24 17:56:51 +00:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
2021-08-31 09:40:28 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
2022-03-24 17:56:51 +00:00
|
|
|
namespace ASC.Data.Backup
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
|
|
|
public class BackupFileUploadHandler
|
|
|
|
{
|
|
|
|
private const long MaxBackupFileSize = 1024L * 1024L * 1024L;
|
|
|
|
|
2022-03-24 17:56:51 +00:00
|
|
|
public BackupFileUploadHandler(RequestDelegate next)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
|
|
|
|
2022-03-24 17:56:51 +00:00
|
|
|
}
|
|
|
|
public async Task Invoke(HttpContext context,
|
|
|
|
PermissionContext permissionContext,
|
|
|
|
BackupAjaxHandler backupAjaxHandler)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-03-24 17:56:51 +00:00
|
|
|
FileUploadResult result = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (context.Request.Form.Files.Count == 0)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-03-24 17:56:51 +00:00
|
|
|
result = Error("No files.");
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 17:56:51 +00:00
|
|
|
if (!permissionContext.CheckPermissions(SecutiryConstants.EditPortalSettings))
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-03-24 17:56:51 +00:00
|
|
|
result = Error("Access denied.");
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 17:56:51 +00:00
|
|
|
var file = context.Request.Form.Files[0];
|
|
|
|
|
2021-08-31 09:40:28 +00:00
|
|
|
if (file.Length <= 0 || file.Length > MaxBackupFileSize)
|
|
|
|
{
|
2022-03-24 17:56:51 +00:00
|
|
|
result = Error($"File size must be greater than 0 and less than {MaxBackupFileSize} bytes");
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 17:56:51 +00:00
|
|
|
|
|
|
|
var filePath = backupAjaxHandler.GetTmpFilePath();
|
2021-08-31 09:40:28 +00:00
|
|
|
|
|
|
|
if (File.Exists(filePath))
|
|
|
|
{
|
|
|
|
File.Delete(filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
using (var fileStream = File.Create(filePath))
|
|
|
|
{
|
2022-03-24 17:56:51 +00:00
|
|
|
await file.CopyToAsync(fileStream);
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 17:56:51 +00:00
|
|
|
result = Success();
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
2022-03-24 17:56:51 +00:00
|
|
|
result = Error(error.Message);
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-03-24 17:56:51 +00:00
|
|
|
|
|
|
|
await context.Response.WriteAsync(JsonSerializer.Serialize(result));
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 17:56:51 +00:00
|
|
|
private FileUploadResult Success()
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-03-24 17:56:51 +00:00
|
|
|
return new FileUploadResult
|
|
|
|
{
|
|
|
|
Success = true
|
|
|
|
};
|
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-03-24 17:56:51 +00:00
|
|
|
private FileUploadResult Error(string messageFormat, params object[] args)
|
|
|
|
{
|
|
|
|
return new FileUploadResult
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-03-24 17:56:51 +00:00
|
|
|
Success = false,
|
|
|
|
Message = string.Format(messageFormat, args)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-03-24 17:56:51 +00:00
|
|
|
public static class BackupFileUploadHandlerExtensions
|
|
|
|
{
|
|
|
|
public static IApplicationBuilder UseBackupFileUploadHandler(this IApplicationBuilder builder)
|
|
|
|
{
|
|
|
|
return builder.UseMiddleware<BackupFileUploadHandler>();
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-24 17:41:31 +00:00
|
|
|
}
|