213 lines
9.1 KiB
C#
213 lines
9.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using System.Web.Http;
|
|
using Weee.Service;
|
|
using Microsoft.AspNet.Identity;
|
|
using System.Web.Http.Results;
|
|
using Resources;
|
|
using NLog;
|
|
|
|
namespace Weee.Controllers
|
|
{
|
|
public class FileController : ApiControllerBase
|
|
{
|
|
private readonly WeeeDataAuthorizeService service;
|
|
private readonly WeeeUserTemplatesService templateService;//CFT-89
|
|
public FileController(WeeeDataAuthorizeService s, WeeeUserTemplatesService ts)
|
|
: base(s)
|
|
{
|
|
service = s;
|
|
templateService = ts;
|
|
}
|
|
// POST api/<controller>
|
|
[Route("api/Upload/")]
|
|
[HttpPost]
|
|
public List<string> Post()
|
|
{
|
|
//Logger log = NLog.LogManager.GetCurrentClassLogger();
|
|
//log.Info("api/Upload 1");
|
|
var httpRequest = HttpContext.Current.Request;
|
|
if (httpRequest.Files.Count > 0)
|
|
{
|
|
// service.GetUserContext().CompanyID
|
|
var docfiles = new List<string>();
|
|
var storage = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(Storage.AzureStorage)) as Storage.AzureStorage;
|
|
foreach (string file in httpRequest.Files)
|
|
{
|
|
var postedFile = httpRequest.Files[file];
|
|
var fileName = postedFile.FileName;
|
|
var mkdir = "";
|
|
if (file.Contains("_lca"))
|
|
{
|
|
var arr = file.Split('_');
|
|
fileName = arr[0] + "_" + fileName;
|
|
mkdir = arr[1];
|
|
}
|
|
//log.Info("api/Upload 2");
|
|
var uri = storage.SaveToAzure(postedFile.InputStream, fileName, getBaseUrl(), mkdir);//DL-5
|
|
//log.Info($"api/Upload uri {uri}");
|
|
docfiles.Add(uri.ToString().Substring(uri.ToString().IndexOf("Browser_Local") - 1));//DL-5
|
|
//docfiles.Add(storage.SaveToAzure(postedFile.InputStream,postedFile.FileName).ToString());//DL-5
|
|
}
|
|
//log.Info("api/Upload 4");
|
|
return docfiles;
|
|
}
|
|
|
|
return new List<string>();
|
|
}
|
|
/// <summary>
|
|
/// 拿來做組織型範本檔案上傳
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("api/UploadUserFiles/")]
|
|
[HttpPost]
|
|
public object UploadUserFiles()
|
|
{
|
|
string returnMsg = "";
|
|
var httpRequest = HttpContext.Current.Request;
|
|
if (httpRequest.Files.Count > 0)
|
|
{
|
|
string loginId = User.Identity.GetUserName();
|
|
string userId = User.Identity.GetUserId();
|
|
// service.GetUserContext().CompanyID
|
|
var docfiles = new List<string>();
|
|
var storage = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(Storage.AzureStorage)) as Storage.AzureStorage;
|
|
foreach (string file in httpRequest.Files)
|
|
{
|
|
var postedFile = httpRequest.Files[file];
|
|
var uri = storage.SaveToAzure(postedFile.InputStream, postedFile.FileName, getBaseUrl(), loginId, false
|
|
, CScommon.ProgramConstants.OrganizationWordReport);// "OrganizationLCADocTemplate.docx");
|
|
if(uri.ToString().Contains("Browser_Local"))
|
|
{
|
|
string fileName = uri.ToString().Substring(uri.ToString().IndexOf("Browser_Local") - 1);
|
|
templateService.SaveUserTemplate(userId, fileName);
|
|
returnMsg = Resource.UploadFileDone;
|
|
}
|
|
}
|
|
return Json(new { Success = true, Msg = returnMsg });
|
|
}
|
|
return Json(new { Success = false, Msg = Resource.UploadFileFail});
|
|
}
|
|
/// <summary>
|
|
/// 拿來做組織型盤查報告壓縮檔上傳
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("api/UploadLcaFiles/{isProductLCA}")]
|
|
[HttpPost]
|
|
public object UploadLcaFiles(bool isProductLCA)
|
|
{
|
|
string returnMsg = "";
|
|
var httpRequest = HttpContext.Current.Request;
|
|
int lcaId = 0;
|
|
|
|
try
|
|
{
|
|
if (httpRequest.Form[0] != null && httpRequest.Form[0].Trim() != "")
|
|
{
|
|
lcaId = Convert.ToInt32(httpRequest.Form[0]);
|
|
}
|
|
if (httpRequest.Files.Count > 0)
|
|
{
|
|
string loginId = User.Identity.GetUserName();
|
|
//string userId = User.Identity.GetUserId();
|
|
// service.GetUserContext().CompanyID
|
|
var docfiles = new List<string>();
|
|
string lca = lcaId + "";
|
|
var storage = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(Storage.AzureStorage)) as Storage.AzureStorage;
|
|
foreach (string file in httpRequest.Files)
|
|
{
|
|
var postedFile = httpRequest.Files[file];
|
|
Uri uri;
|
|
if(isProductLCA)
|
|
uri = storage.SaveToAzure(postedFile.InputStream, postedFile.FileName, getBaseUrl(), "lca" + lcaId, false
|
|
, $"{CScommon.ProgramConstants.ProductUploadReport}");
|
|
else
|
|
uri = storage.SaveToAzure(postedFile.InputStream, postedFile.FileName, getBaseUrl(), "lca" + lcaId, false
|
|
, $"{CScommon.ProgramConstants.OrganizationUploadReport}");
|
|
if (uri.ToString().Contains("Browser_Local"))
|
|
{
|
|
string fileName = uri.ToString().Substring(uri.ToString().IndexOf("Browser_Local") - 1);
|
|
templateService.SaveUploadZipRreport(lcaId, fileName);
|
|
returnMsg = Resource.UploadFileDone;
|
|
}
|
|
}
|
|
return Json(new { Success = true, Msg = returnMsg });
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return Json(new { Success = false, Msg = Resource.UploadFileFail });
|
|
throw;
|
|
}
|
|
return Json(new { Success = false, Msg = Resource.UploadFileFail });
|
|
}
|
|
/// <summary>
|
|
/// 拿來做組織型稽核查證回覆壓縮檔上傳(可能用不到)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("api/ReplyLcaFiles/")]
|
|
[HttpPost]
|
|
public object ReplyLcaFiles()
|
|
{
|
|
string returnMsg = "";
|
|
var httpRequest = HttpContext.Current.Request;
|
|
if (httpRequest.Files.Count > 0)
|
|
{
|
|
//string loginId = User.Identity.GetUserName();
|
|
//string userId = User.Identity.GetUserId();
|
|
// service.GetUserContext().CompanyID
|
|
var docfiles = new List<string>();
|
|
string lca = HttpContext.Current.Session["LCAid"] + "";
|
|
var storage = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(Storage.AzureStorage)) as Storage.AzureStorage;
|
|
foreach (string file in httpRequest.Files)
|
|
{
|
|
var postedFile = httpRequest.Files[file];
|
|
var uri = storage.SaveToAzure(postedFile.InputStream, postedFile.FileName, getBaseUrl(), lca, false
|
|
, $"{CScommon.ProgramConstants.OrganizationDownloadReply}.zip");
|
|
if (uri.ToString().Contains("Browser_Local"))
|
|
{
|
|
string fileName = uri.ToString().Substring(uri.ToString().IndexOf("Browser_Local") - 1);
|
|
//templateService.SaveUserTemplate(userId, fileName);
|
|
returnMsg = Resource.UploadFileDone;
|
|
}
|
|
}
|
|
return Json(new { Success = true, Msg = returnMsg });
|
|
}
|
|
return Json(new { Success = false, Msg = Resource.UploadFileFail });
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 下載查證單位回復的壓縮檔
|
|
/// </summary>
|
|
/// <param name="LCAID"></param>
|
|
/// <returns></returns>
|
|
[Route("api/DownloadZipReplyReport/{LCAID}")]
|
|
public string DownloadZipReplyReport(int LCAID)
|
|
{
|
|
string downloadUrl = templateService.DownloadZipReplyReport(LCAID, getBaseUrl());
|
|
return downloadUrl;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 下載原上傳給查證單位的壓縮檔
|
|
/// </summary>
|
|
/// <param name="LCAID"></param>
|
|
/// <returns></returns>
|
|
[Route("api/DownloadZipReport/{LCAID}")]
|
|
public string DownloadZipReport(int LCAID)
|
|
{
|
|
string downloadUrl = templateService.DownloadZipReport(LCAID, getBaseUrl());
|
|
return downloadUrl;
|
|
|
|
}
|
|
}
|
|
} |