Files
document-server-integration/web/documentserver-example/csharp-mvc/Helpers/DocManagerHelper.cs
2018-03-01 12:41:44 +03:00

154 lines
5.4 KiB
C#

/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
using System.Web.Configuration;
using OnlineEditorsExampleMVC.Models;
namespace OnlineEditorsExampleMVC.Helpers
{
public class DocManagerHelper
{
public static long MaxFileSize
{
get
{
long size;
long.TryParse(WebConfigurationManager.AppSettings["filesize-max"], out size);
return size > 0 ? size : 5 * 1024 * 1024;
}
}
public static List<string> FileExts
{
get { return ViewedExts.Concat(EditedExts).Concat(ConvertExts).ToList(); }
}
public static List<string> ViewedExts
{
get { return (WebConfigurationManager.AppSettings["files.docservice.viewed-docs"] ?? "").Split(new char[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); }
}
public static List<string> EditedExts
{
get { return (WebConfigurationManager.AppSettings["files.docservice.edited-docs"] ?? "").Split(new char[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); }
}
public static List<string> ConvertExts
{
get { return (WebConfigurationManager.AppSettings["files.docservice.convert-docs"] ?? "").Split(new char[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); }
}
public static string CurUserHostAddress(string userAddress = null)
{
return Regex.Replace(userAddress ?? HttpContext.Current.Request.UserHostAddress, "[^0-9a-zA-Z.=]", "_");
}
public static string StoragePath(string fileName, string userAddress = null)
{
var directory = HttpRuntime.AppDomainAppPath + CurUserHostAddress(userAddress) + "\\";
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
return directory + fileName;
}
public static string GetCorrectName(string fileName)
{
var baseName = Path.GetFileNameWithoutExtension(fileName);
var ext = Path.GetExtension(fileName);
var name = baseName + ext;
for (var i = 1; File.Exists(StoragePath(name)); i++)
{
name = baseName + " (" + i + ")" + ext;
}
return name;
}
public static string CreateDemo(string fileExt)
{
var demoName = "sample." + fileExt;
var fileName = GetCorrectName(demoName);
File.Copy(HttpRuntime.AppDomainAppPath + "app_data\\" + demoName, StoragePath(fileName));
return fileName;
}
public static string GetFileUri(string fileName)
{
var uri = new UriBuilder(HttpContext.Current.Request.Url)
{
Path = HttpRuntime.AppDomainAppVirtualPath + "/"
+ CurUserHostAddress() + "/"
+ fileName,
Query = ""
};
return uri.ToString();
}
public static string GetCallback(string fileName)
{
var callbackUrl = new UriBuilder(HttpContext.Current.Request.Url)
{
Path =
HttpRuntime.AppDomainAppVirtualPath
+ (HttpRuntime.AppDomainAppVirtualPath.EndsWith("/") ? "" : "/")
+ "webeditor.ashx",
Query = "type=track"
+ "&fileName=" + HttpUtility.UrlEncode(fileName)
+ "&userAddress=" + HttpUtility.UrlEncode(HttpContext.Current.Request.UserHostAddress)
};
return callbackUrl.ToString();
}
public static string GetInternalExtension(FileUtility.FileType fileType)
{
switch (fileType)
{
case FileUtility.FileType.Text:
return ".docx";
case FileUtility.FileType.Spreadsheet:
return ".xlsx";
case FileUtility.FileType.Presentation:
return ".pptx";
default:
return ".docx";
}
}
}
}