Added method to upload and delete documents from service records.
This commit is contained in:
57
Helper/FileHelper.cs
Normal file
57
Helper/FileHelper.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
namespace CarCareTracker.Helper
|
||||
{
|
||||
public interface IFileHelper
|
||||
{
|
||||
public string MoveFileFromTemp(string currentFilePath, string newFolder);
|
||||
public bool DeleteFile(string currentFilePath);
|
||||
}
|
||||
public class FileHelper: IFileHelper
|
||||
{
|
||||
private readonly IWebHostEnvironment _webEnv;
|
||||
public FileHelper(IWebHostEnvironment webEnv)
|
||||
{
|
||||
_webEnv = webEnv;
|
||||
}
|
||||
public string MoveFileFromTemp(string currentFilePath, string newFolder)
|
||||
{
|
||||
string tempPath = "temp/";
|
||||
if (!currentFilePath.StartsWith("/temp/")) //file is not in temp directory.
|
||||
{
|
||||
return currentFilePath;
|
||||
}
|
||||
if (currentFilePath.StartsWith("/")) {
|
||||
currentFilePath = currentFilePath.Substring(1);
|
||||
}
|
||||
string uploadPath = Path.Combine(_webEnv.WebRootPath, newFolder);
|
||||
string oldFilePath = Path.Combine(_webEnv.WebRootPath, currentFilePath);
|
||||
if (!Directory.Exists(uploadPath))
|
||||
Directory.CreateDirectory(uploadPath);
|
||||
string newFileUploadPath = oldFilePath.Replace(tempPath, newFolder);
|
||||
if (File.Exists(oldFilePath))
|
||||
{
|
||||
File.Move(oldFilePath, newFileUploadPath);
|
||||
}
|
||||
string newFilePathToReturn = "/" + currentFilePath.Replace(tempPath, newFolder);
|
||||
return newFilePathToReturn;
|
||||
}
|
||||
public bool DeleteFile(string currentFilePath)
|
||||
{
|
||||
if (currentFilePath.StartsWith("/"))
|
||||
{
|
||||
currentFilePath = currentFilePath.Substring(1);
|
||||
}
|
||||
string filePath = Path.Combine(_webEnv.WebRootPath, currentFilePath);
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
File.Delete(filePath);
|
||||
}
|
||||
if (!File.Exists(filePath)) //verify file no longer exists.
|
||||
{
|
||||
return true;
|
||||
} else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user