69 lines
2.5 KiB
C#
69 lines
2.5 KiB
C#
using Knoks.Framework.Extentions;
|
|
using System;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Knoks.Framework.Document
|
|
{
|
|
public class DocumentProcess : IDocumentProcess
|
|
{
|
|
private IDocumentOptions _documentOptions;
|
|
|
|
public DocumentProcess(IDocumentOptions documentOptions)
|
|
{
|
|
_documentOptions = documentOptions;
|
|
}
|
|
|
|
public async Task Save(Stream srcStream, string dstFileName, bool replace = false)
|
|
{
|
|
var destinationFileInfo = new FileInfo(Path.Combine(_documentOptions.UploadDirectoryPath, dstFileName));
|
|
bool replaced = replace ? await destinationFileInfo.ArchiveFile() : false;
|
|
if (!replaced)
|
|
destinationFileInfo.CreateDirectory();
|
|
|
|
using (var fs = destinationFileInfo.Create())
|
|
{
|
|
await srcStream.CopyToAsync(fs);
|
|
fs.Flush();
|
|
}
|
|
}
|
|
public async Task<string> SaveIndexed(Stream srcStream, string dstFileName)
|
|
{
|
|
var resultFileName = dstFileName;
|
|
var path = Path.Combine(_documentOptions.UploadDirectoryPath, dstFileName);
|
|
var destinationFileInfo = new FileInfo(path);
|
|
string name = destinationFileInfo.GenerateArchiveFullName();
|
|
if (path != name)
|
|
{
|
|
destinationFileInfo = new FileInfo(name);
|
|
resultFileName = Path.Combine(Path.GetDirectoryName(dstFileName), Path.GetFileName(name));
|
|
}
|
|
//if(resultFileName==dstFileName)
|
|
destinationFileInfo.CreateDirectory();
|
|
using (var fs = destinationFileInfo.Create())
|
|
{
|
|
await srcStream.CopyToAsync(fs);
|
|
fs.Flush();
|
|
}
|
|
return resultFileName;
|
|
}
|
|
public FileStream LoadFileStream(string dstFileName)
|
|
{
|
|
if (File.Exists(Path.Combine(_documentOptions.DownloadDirectoryPath, dstFileName)))
|
|
{
|
|
return new FileStream(Path.Combine(_documentOptions.DownloadDirectoryPath, dstFileName), FileMode.Open, FileAccess.Read);
|
|
}
|
|
return null;
|
|
}
|
|
public DateTimeOffset GetFileModified(string dstFileName)
|
|
{
|
|
return new FileInfo(Path.Combine(_documentOptions.DownloadDirectoryPath, dstFileName)).CreationTimeUtc;
|
|
}
|
|
|
|
public byte[] LoadFileBytes(string dstFileName)
|
|
{
|
|
return File.ReadAllBytes(Path.Combine(_documentOptions.DownloadDirectoryPath, dstFileName));
|
|
}
|
|
}
|
|
}
|