215 lines
8.7 KiB
C#
215 lines
8.7 KiB
C#
using System.Net.Mail;
|
|
using System.Net;
|
|
using Microsoft.AspNetCore.Components.Forms;
|
|
using SeaHavenIndustries.ViewModel;
|
|
using Amazon.S3;
|
|
using Amazon.S3.Model;
|
|
using Amazon.Runtime;
|
|
|
|
namespace SeaHavenIndustries.Helper
|
|
{
|
|
public class UploadFileHp
|
|
{
|
|
private readonly IWebHostEnvironment _hosting;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
private readonly IAmazonS3 _s3Client;
|
|
|
|
public UploadFileHp(IWebHostEnvironment hosting, IHttpContextAccessor httpContextAccessor, IAmazonS3 s3Client)
|
|
{
|
|
|
|
_hosting = hosting;
|
|
_httpContextAccessor = httpContextAccessor;
|
|
var awsCredentials = new BasicAWSCredentials("AKIAUY6EP2OAGEFSEP4W", "7xF+1RoO0pqyAWu/Lt9zP3z5HvHS/UjcCx2KbDbc");
|
|
|
|
_s3Client = new AmazonS3Client(awsCredentials, Amazon.RegionEndpoint.USEast2);
|
|
|
|
}
|
|
|
|
//public async Task<UploadResponseVm> UploadFiles(IBrowserFile file, string? path)
|
|
//{
|
|
// try
|
|
// {
|
|
// string fileName = Path.GetFileName(file.Name);
|
|
// string uniqueFileName = $"{Guid.NewGuid()}_{fileName}";
|
|
// // Define the path where the image will be stored in the server
|
|
// string uploadProfilePath = Path.Combine("assets", "UploadDocuments");
|
|
// string fullPath = Path.Combine(_hosting.WebRootPath, uploadProfilePath, uniqueFileName);
|
|
// // Create the directory if it doesn't exist
|
|
// Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
|
|
// // Copy the file to the server
|
|
// using (var fileStream = new FileStream(fullPath, FileMode.Create))
|
|
// {
|
|
// await file.OpenReadStream(int.MaxValue).CopyToAsync(fileStream);
|
|
// }
|
|
// // Check if file path is not null, and delete the file if needed
|
|
// var request = _httpContextAccessor.HttpContext.Request;
|
|
// var domain = $"{request.Scheme}://{request.Host}";
|
|
// if (path != null)
|
|
// {
|
|
// path = path.Replace(domain, _hosting.WebRootPath);
|
|
// if (!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
|
|
// {
|
|
// System.IO.File.Delete(path);
|
|
// }
|
|
// }
|
|
// // Update the user's image URL
|
|
// var dbpath = domain+ "/" + Path.Combine(uploadProfilePath, uniqueFileName);
|
|
// var response = new UploadResponseVm
|
|
// {
|
|
// StatusCode = 200,
|
|
// Domain = dbpath,
|
|
// FileName = fileName,
|
|
// Message = "File uploaded successfully"
|
|
// };
|
|
|
|
// return response;
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// return new UploadResponseVm
|
|
// {
|
|
// StatusCode = 500, // or any appropriate status code for an internal server error
|
|
// Message = ex.Message
|
|
// };
|
|
// }
|
|
//}
|
|
public async Task<UploadResponseVm> UploadFilescustomuploadfile(IBrowserFile file, string? path,string folderpath)
|
|
{
|
|
try
|
|
{
|
|
string fileName = Path.GetFileName(file.Name);
|
|
string uniqueFileName = $"{Guid.NewGuid()}_{fileName}";
|
|
// Define the path where the image will be stored in the server
|
|
string uploadProfilePath = folderpath;
|
|
string fullPath = Path.Combine(_hosting.WebRootPath, uploadProfilePath, uniqueFileName);
|
|
// Create the directory if it doesn't exist
|
|
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
|
|
// Copy the file to the server
|
|
using (var fileStream = new FileStream(fullPath, FileMode.Create))
|
|
{
|
|
await file.OpenReadStream(int.MaxValue).CopyToAsync(fileStream);
|
|
}
|
|
// Check if file path is not null, and delete the file if needed
|
|
var request = _httpContextAccessor.HttpContext.Request;
|
|
var domain = $"{request.Scheme}://{request.Host}";
|
|
if (path != null)
|
|
{
|
|
path = path.Replace(domain, _hosting.WebRootPath);
|
|
if (!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
|
|
{
|
|
System.IO.File.Delete(path);
|
|
}
|
|
}
|
|
// Update the user's image URL
|
|
var dbpath = domain+ "/" + Path.Combine(uploadProfilePath, uniqueFileName);
|
|
var response = new UploadResponseVm
|
|
{
|
|
StatusCode = 200,
|
|
Domain = dbpath,
|
|
FileName = fileName,
|
|
Message = "File uploaded successfully"
|
|
};
|
|
|
|
return response;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new UploadResponseVm
|
|
{
|
|
StatusCode = 500, // or any appropriate status code for an internal server error
|
|
Message = ex.Message
|
|
};
|
|
}
|
|
}
|
|
|
|
//public async Task<string> UploadFileAsync(byte[] fileContent)
|
|
//{
|
|
// using (MemoryStream memoryStream = new MemoryStream(fileContent))
|
|
// {
|
|
// var request = new PutObjectRequest
|
|
// {
|
|
// BucketName = "seahaven-attachments",
|
|
// Key = "s3://seahaven-attachments",
|
|
// InputStream = memoryStream,
|
|
// ContentType = "application/octet-stream"
|
|
// };
|
|
|
|
// var response = await _s3Client.PutObjectAsync(request);
|
|
// // Handle the response as needed
|
|
|
|
// return "ff";
|
|
// }
|
|
//}
|
|
|
|
public async Task<UploadResponseVm> UploadFiles(IBrowserFile file,string? path)
|
|
{
|
|
//FileStream fs = File.Open(file, FileMode.Open);
|
|
try
|
|
{
|
|
string fileExtension = GetFileExtension(file.Name);
|
|
|
|
using (var memoryStream = new MemoryStream())
|
|
{
|
|
await file.OpenReadStream().CopyToAsync(memoryStream);
|
|
var request = new PutObjectRequest
|
|
{
|
|
BucketName = "seahaven-attachments",
|
|
Key = file.Name,
|
|
InputStream = memoryStream,
|
|
ContentType = "application/" + fileExtension
|
|
};
|
|
|
|
var response = await _s3Client.PutObjectAsync(request);
|
|
// Handle the response as needed
|
|
if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
|
|
{
|
|
// Construct the object URL
|
|
string objectUrl = $"https://seahaven-attachments.s3.us-east-2.amazonaws.com/{file.Name}";
|
|
|
|
// Optionally, you can return the object URL or use it as needed
|
|
var response1 = new UploadResponseVm
|
|
{
|
|
StatusCode = 200,
|
|
Domain = objectUrl,
|
|
FileName = file.Name,
|
|
Message = "File uploaded successfully"
|
|
};
|
|
return response1;
|
|
}
|
|
else
|
|
{
|
|
return new UploadResponseVm
|
|
{
|
|
StatusCode = 500, // or any appropriate status code for an internal server error
|
|
Message = "Some thing went wrong"
|
|
};
|
|
}
|
|
}
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
return new UploadResponseVm
|
|
{
|
|
StatusCode = 500, // or any appropriate status code for an internal server error
|
|
Message = ex.Message
|
|
};
|
|
}
|
|
|
|
|
|
}
|
|
private string GetFileExtension(string fileName)
|
|
{
|
|
if (!string.IsNullOrEmpty(fileName))
|
|
{
|
|
int lastDotIndex = fileName.LastIndexOf('.');
|
|
if (lastDotIndex != -1 && lastDotIndex < fileName.Length - 1)
|
|
{
|
|
return fileName.Substring(lastDotIndex + 1);
|
|
}
|
|
}
|
|
|
|
return string.Empty; // or handle accordingly if no extension is found
|
|
}
|
|
}
|
|
|
|
}
|