1、文件存储接口
using System.IO;
using System.Threading.Tasks;
namespace CJavaPy.Storage
{
public interface IStorage
{
string SaveFile(Stream stream, string fileName);
Task<string> SaveFileAsync(Stream stream, string fileName);
string SaveFile(Stream stream, string directory, string fileName);
Task<string> SaveFileAsync(Stream stream, string directory, string fileName);
void AppendFile(Stream stream, string filePath);
Task AppendFileAsync(Stream stream, string filePath);
void Delete(string filePath);
void DeleteDirectory(string path);
}
}
2、本地存储实现接口方法
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace CJavaPy.Storage
{
public class LocalStorage : IStorage
{
public string ToFilePath(string path)
{
return Path.Combine(path.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries));
}
protected virtual string MapPath(string path)
{
return Path.Combine(Directory.GetCurrentDirectory(), ToFilePath(path.TrimStart('~')));
}
protected virtual string GenerateDictionary(string fileName)
{
return Path.Combine("Files", fileName);
}
public virtual void AppendFile(Stream stream, string filePath)
{
using (FileStream fileStream = new FileStream(MapPath(filePath), FileMode.Append))
{
stream.CopyTo(fileStream);
}
}
public virtual async Task AppendFileAsync(Stream stream, string filePath)
{
using (FileStream fileStream = new FileStream(MapPath(filePath), FileMode.Append))
{
await stream.CopyToAsync(fileStream);
}
}
public virtual void Delete(string filePath)
{
File.Delete(MapPath(filePath));
}
public virtual string SaveFile(Stream stream, string fileName)
{
return SaveFile(stream, GenerateDictionary(fileName), fileName);
}
public virtual Task<string> SaveFileAsync(Stream stream, string fileName)
{
return SaveFileAsync(stream, GenerateDictionary(fileName), fileName);
}
public virtual string SaveFile(Stream stream, string directory, string fileName)
{
string filePath = Path.Combine(MapPath(directory), fileName);
using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
{
stream.CopyTo(fileStream);
}
return filePath;
}
public virtual async Task<string> SaveFileAsync(Stream stream, string directory, string fileName)
{
string filePath = Path.Combine(MapPath(directory), fileName);
using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
{
await stream.CopyToAsync(fileStream);
}
return filePath;
}
public virtual void DeleteDirectory(string path)
{
Directory.Delete(MapPath(path), true);
}
}
}
3、Web目录(wwwroot)存储实现接口方法
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace CJavaPy.Storage
{
public class WebStorage : IStorage
{
const string WebRoot = "wwwroot";
const string UploadFolder = "UpLoad";
const string ImageFolder = "Images";
const string FileFolder = "Files";
public bool IsImage(string extension)
{
extension = extension.ToLower();
if (extension == ".gif" || extension == ".jpg" || extension == ".png" || extension == ".jpeg" || extension == ".bmp")
{
return true;
}
else return false;
}
public string ToFilePath(string path)
{
return Path.Combine(path.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries));
}
protected virtual string MapPath(string path)
{
return Path.Combine(Directory.GetCurrentDirectory(), WebRoot, ToFilePath(path.TrimStart('~')));
}
protected virtual string GenerateDictionary(string fileName)
{
string folder = IsImage(Path.GetExtension(fileName)) ? ImageFolder : FileFolder;
return Path.Combine(new string[] { UploadFolder, folder, DateTime.Now.ToString("yyyyMM") });
}
public virtual void AppendFile(Stream stream, string filePath)
{
using (FileStream fileStream = new FileStream(MapPath(filePath), FileMode.Append))
{
stream.CopyTo(fileStream);
}
}
public virtual async Task AppendFileAsync(Stream stream, string filePath)
{
using (FileStream fileStream = new FileStream(MapPath(filePath), FileMode.Append))
{
await stream.CopyToAsync(fileStream);
}
}
public virtual void Delete(string filePath)
{
FileInfo file = new FileInfo(MapPath(filePath));
if (file.Exists)
{
file.Delete();
}
}
public virtual string SaveFile(Stream stream, string fileName)
{
return SaveFile(stream, GenerateDictionary(fileName), fileName);
}
public virtual Task<string> SaveFileAsync(Stream stream, string fileName)
{
return SaveFileAsync(stream, GenerateDictionary(fileName), fileName);
}
public virtual string SaveFile(Stream stream, string directory, string fileName)
{
string filePath = Path.Combine(MapPath(directory), fileName);
DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(filePath));
if (!dir.Exists)
{
dir.Create();
}
using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
{
stream.CopyTo(fileStream);
}
string webPath = string.Join("/", directory.Split(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries));
return $"~/{webPath}/{fileName}";
}
public virtual async Task<string> SaveFileAsync(Stream stream, string directory, string fileName)
{
string filePath = Path.Combine(MapPath(directory), fileName);
DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(filePath));
if (!dir.Exists)
{
dir.Create();
}
using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
{
await stream.CopyToAsync(fileStream);
}
string webPath = string.Join("/", directory.Split(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries));
return $"~/{webPath}/{fileName}";
}
public virtual void DeleteDirectory(string path)
{
Directory.Delete(MapPath(path), true);
}
}
}