446 lines
16 KiB
C#
446 lines
16 KiB
C#
using CMS_CORE_Library.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using System.Web.Http;
|
|
using Thermo.Active.Model.DTOModels;
|
|
using Thermo.Active.NC;
|
|
using static CMS_CORE_Library.Models.DataStructures;
|
|
using static Thermo.Active.Config.ServerConfig;
|
|
using static Thermo.Active.Model.Constants;
|
|
|
|
namespace Thermo.Active.Controllers.WebApi
|
|
{
|
|
[RoutePrefix("api/file_manager")]
|
|
public class NcFileController : ApiController
|
|
{
|
|
/// <summary>
|
|
/// Oggetto adapter condiviso da WebAPI
|
|
/// </summary>
|
|
protected static NcFileAdapter ncAdapter = new NcFileAdapter();
|
|
|
|
[Route("files"), HttpGet]
|
|
public IHttpActionResult GetFileList(string filePath = "")
|
|
{
|
|
ncAdapter.Connect();
|
|
|
|
CmsError libraryError = ncAdapter.GetFileList(filePath, out List<PreviewFileModel> fileList);
|
|
if (libraryError.IsError())
|
|
return BadRequest(libraryError.localizationKey);
|
|
|
|
return Ok(fileList);
|
|
}
|
|
|
|
[Route("file/info"), HttpGet]
|
|
public IHttpActionResult GetFileInfo([FromUri]string filePath)
|
|
{
|
|
ncAdapter.Connect();
|
|
|
|
CmsError libraryError = ncAdapter.GetFileInfo(filePath, out InfoFile fileInfo);
|
|
if (libraryError.IsError())
|
|
return BadRequest(libraryError.localizationKey);
|
|
|
|
return Ok(fileInfo);
|
|
}
|
|
|
|
[Route("file/active/info"), HttpGet]
|
|
public IHttpActionResult GetActiveFileInfo([FromUri]string filePath)
|
|
{
|
|
ncAdapter.Connect();
|
|
|
|
CmsError libraryError = ncAdapter.GetActiveFileInfo(filePath, out DTOActiveImageAndNameDataModel fileInfo);
|
|
if (libraryError.IsError())
|
|
return BadRequest(libraryError.localizationKey);
|
|
|
|
return Ok(fileInfo);
|
|
}
|
|
|
|
[Route("file/active"), HttpPut]
|
|
public IHttpActionResult SetActiveProgram([FromUri]string filePath)
|
|
{
|
|
ncAdapter.Connect();
|
|
|
|
CmsError libraryError = ncAdapter.SetActiveProgramInfo(filePath, out DTOActiveProgramDataModel fileInfo);
|
|
if (libraryError.IsError())
|
|
return BadRequest(libraryError.localizationKey);
|
|
|
|
return Ok(fileInfo);
|
|
}
|
|
|
|
[Route("file/deactivate"), HttpPut]
|
|
public IHttpActionResult DeactivateProgram()
|
|
{
|
|
ncAdapter.Connect();
|
|
|
|
CmsError libraryError = ncAdapter.DeactivateProgram(out DTOActiveProgramDataModel fileInfo);
|
|
if (libraryError.IsError())
|
|
return BadRequest(libraryError.localizationKey);
|
|
|
|
return Ok(fileInfo);
|
|
}
|
|
|
|
[Route("upload"), HttpPost]
|
|
public async Task<IHttpActionResult> UpdloadProgramWithImage()
|
|
{
|
|
|
|
bool imageUploaded = false;
|
|
string mainPPName = "";
|
|
List<string> programs = new List<string>();
|
|
|
|
if (!Request.Content.IsMimeMultipartContent())
|
|
{
|
|
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
|
|
}
|
|
|
|
List<FormItem> formItems = await MultipartHandler.ManageMultiPartFileAsync(Request);
|
|
using (NcFileAdapter ncFileHandler = new NcFileAdapter())
|
|
{
|
|
ncFileHandler.Connect();
|
|
|
|
if (NcConfig.NcVendor == NC_VENDOR.OSAI)
|
|
{
|
|
// Clean upload folder only OSAI
|
|
CmsError libraryError = ncFileHandler.CleanUploadFolder();
|
|
if (libraryError.IsError() && libraryError.errorCode != CMS_ERROR_CODES.FILE_NOT_FOUND)
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
ActiveProgramDataModel programData = new ActiveProgramDataModel();
|
|
foreach (FormItem item in formItems)
|
|
{
|
|
if (item.IsAFile)
|
|
{
|
|
if (item.ParameterName == "main")
|
|
{
|
|
File.WriteAllBytes(TEMP_PP_FOLDER + item.FileName, item.Data);
|
|
programs.Add(item.FileName);
|
|
|
|
// Upload main program
|
|
CmsError libraryError = ncFileHandler.UploadPartProgramAndActivate(TEMP_PP_FOLDER, item.FileName, out programData);
|
|
if (libraryError.IsError())
|
|
return BadRequest(libraryError.localizationKey);
|
|
|
|
mainPPName = Path.GetFileNameWithoutExtension(programData.Path);
|
|
}
|
|
else if (item.ParameterName == "file")
|
|
{
|
|
File.WriteAllBytes(TEMP_PP_FOLDER + item.FileName, item.Data);
|
|
programs.Add(item.FileName);
|
|
|
|
// Upload files
|
|
CmsError libraryError = ncFileHandler.UploadPartProgram(TEMP_PP_FOLDER, item.FileName, out string programPath);
|
|
if (libraryError.IsError())
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
else if (item.ParameterName == "image")
|
|
{
|
|
// Delete duplicated images
|
|
string[] imgFiles = Directory.GetFiles(PART_PRG_IMAGES);
|
|
foreach (string f in imgFiles)
|
|
{
|
|
if (Path.GetFileNameWithoutExtension(f) == mainPPName)
|
|
File.Delete(f);
|
|
}
|
|
|
|
// Upload image
|
|
File.WriteAllBytes(
|
|
PART_PRG_IMAGES + // Path
|
|
mainPPName + // Part program name
|
|
Path.GetExtension(item.FileName), // Extension of the image
|
|
item.Data);
|
|
|
|
imageUploaded = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!imageUploaded)
|
|
{
|
|
// Delete old images with the same name of the main program
|
|
string[] imgFiles = Directory.GetFiles(PART_PRG_IMAGES);
|
|
foreach (string f in imgFiles)
|
|
{
|
|
if (Path.GetFileNameWithoutExtension(f) == mainPPName)
|
|
File.Delete(f);
|
|
}
|
|
}
|
|
|
|
|
|
if (NcConfig.NcVendor != NC_VENDOR.OSAI)
|
|
{
|
|
|
|
List<string> lines = new List<string>();
|
|
string outputFileName = NcConfig.SharedPath + PARTPRG_LIST_FILE;
|
|
|
|
foreach (string prog in programs)
|
|
{
|
|
FileInfo fi = new FileInfo(NcConfig.SharedPath + prog);
|
|
if (fi.Exists)
|
|
{
|
|
lines.Add(prog + "|" + fi.CreationTime + "|" + fi.LastWriteTime);
|
|
}
|
|
}
|
|
// Write file
|
|
File.WriteAllLines(outputFileName, lines.ToArray());
|
|
}
|
|
|
|
|
|
}
|
|
|
|
return Ok();
|
|
}
|
|
|
|
[Route("shared_folder_ok"), HttpGet]
|
|
public IHttpActionResult IsSharedFolderOK()
|
|
{
|
|
if (NcConfig.NcVendor == NC_VENDOR.OSAI)
|
|
return Ok(true);
|
|
else
|
|
{
|
|
string fileName = NcConfig.SharedPath + PARTPRG_LIST_FILE;
|
|
int count = Directory.GetFiles(NcConfig.SharedPath).Length;
|
|
|
|
if (!File.Exists(fileName) && count == 0)
|
|
return Ok(true);
|
|
|
|
if (File.Exists(fileName) && count == 1)
|
|
return Ok(false);
|
|
|
|
if (File.Exists(fileName) && count > 1)
|
|
{
|
|
// Write file
|
|
string[] lines = File.ReadAllLines(fileName);
|
|
foreach (string line in lines)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(line))
|
|
{
|
|
string[] linesplitted = line.Split('|');
|
|
if (linesplitted.Length != 3)
|
|
return Ok(false);
|
|
FileInfo fi = new FileInfo(NcConfig.SharedPath + linesplitted[0]);
|
|
if (!fi.Exists)
|
|
return Ok(false);
|
|
if (fi.CreationTime.ToString() != linesplitted[1])
|
|
return Ok(false);
|
|
if (fi.LastWriteTime.ToString() != linesplitted[2])
|
|
return Ok(false);
|
|
}
|
|
}
|
|
return Ok(true);
|
|
}
|
|
return Ok(false);
|
|
}
|
|
}
|
|
|
|
[Route("clean_shared_folder"), HttpPut]
|
|
public IHttpActionResult cleanSharedFolder()
|
|
{
|
|
if (NcConfig.NcVendor == NC_VENDOR.OSAI)
|
|
return Ok(true);
|
|
else
|
|
{
|
|
foreach (string filename in Directory.GetFiles(NcConfig.SharedPath))
|
|
{
|
|
File.Delete(filename);
|
|
}
|
|
}
|
|
return Ok(false);
|
|
}
|
|
|
|
[Route("backup_shared_folder"), HttpPut]
|
|
public IHttpActionResult BackupSharedFolder()
|
|
{
|
|
if (NcConfig.NcVendor == NC_VENDOR.OSAI)
|
|
return Ok(true);
|
|
else
|
|
{
|
|
string newFolder = "C:\\Backup_" + new DirectoryInfo(NcConfig.SharedPath).Name + "\\" + DateTime.Now.ToString("dd-MM-yyyy_HH-mm-ss") + "\\";
|
|
|
|
File.Delete(NcConfig.SharedPath + PARTPRG_LIST_FILE);
|
|
Directory.CreateDirectory(newFolder);
|
|
foreach (string filename in Directory.GetFiles(NcConfig.SharedPath))
|
|
{
|
|
File.Move(filename, newFolder + Path.GetFileName(filename));
|
|
}
|
|
return Ok(true);
|
|
}
|
|
}
|
|
|
|
[Route("shared_files"), HttpGet]
|
|
public IHttpActionResult GetSharedFolderFileList(string sharedPath)
|
|
{
|
|
List<object> filelist = new List<object>();
|
|
// NC - PC shared folder path
|
|
string sharedFullPath = NcConfig.SharedPath + sharedPath;
|
|
|
|
if (!Directory.Exists(sharedFullPath))
|
|
return NotFound();
|
|
// Create list of directories
|
|
foreach (string item in Directory.GetDirectories(sharedFullPath))
|
|
{
|
|
filelist.Add(new
|
|
{
|
|
Name = Path.GetFileName(item),
|
|
AbsolutePath = NcConfig.SharedPath + sharedPath,
|
|
Path = sharedPath,
|
|
IsDirectory = true
|
|
});
|
|
}
|
|
|
|
string sharedNcPath = NcConfig.SharedName + sharedPath.Replace('\\', '/');
|
|
// Add files to list
|
|
foreach (string item in Directory.GetFiles(sharedFullPath))
|
|
{
|
|
if (VALID_FILE_EXTENSIONS.Contains(Path.GetExtension(item).ToLower()))
|
|
filelist.Add(new
|
|
{
|
|
Name = Path.GetFileName(item),
|
|
AbsolutePath = sharedNcPath + "/" + Path.GetFileName(item),
|
|
Path = sharedPath + "\\" + Path.GetFileName(item),
|
|
IsDirectory = false
|
|
});
|
|
}
|
|
|
|
return Ok(filelist);
|
|
}
|
|
|
|
#region Queue Manager
|
|
|
|
[Route("queue/add"), HttpPost]
|
|
public async Task<IHttpActionResult> UploadToQueue()
|
|
{
|
|
if (!Request.Content.IsMimeMultipartContent())
|
|
{
|
|
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
|
|
}
|
|
|
|
List<FormItem> formItems = await MultipartHandler.ManageMultiPartFileAsync(Request);
|
|
DTOQueueModel queueItem = new DTOQueueModel();
|
|
foreach (FormItem item in formItems)
|
|
{
|
|
if (item.IsAFile)
|
|
{
|
|
// Get part program under "file" key
|
|
if (item.ParameterName == "file")
|
|
{
|
|
using (NcFileAdapter ncAdapter = new NcFileAdapter())
|
|
{
|
|
ncAdapter.Connect();
|
|
|
|
File.WriteAllBytes(QUEUE_TMP_FOLDER + item.FileName, item.Data);
|
|
|
|
// Get reps
|
|
var repsParam = formItems.Where(x => x.ParameterName == "reps").FirstOrDefault();
|
|
// Check if reps parameter is null or isn't a valid int
|
|
if (repsParam == null || !int.TryParse(repsParam.Value, out int reps))
|
|
return BadRequest();
|
|
// Upload
|
|
CmsError libraryError = ncAdapter.UploadPartProgramAndAddToQueue(QUEUE_TMP_FOLDER, item.FileName, reps, out queueItem);
|
|
if (libraryError.IsError())
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
}
|
|
else if (item.ParameterName == "image")
|
|
{
|
|
File.WriteAllBytes(PART_PRG_IMAGES + item.FileName, item.Data);
|
|
}
|
|
}
|
|
}
|
|
|
|
return Ok(queueItem);
|
|
}
|
|
|
|
[Route("queue/{processId:int}/remove/{itemId:int}"), HttpDelete]
|
|
public IHttpActionResult RemoveFromQueue(int processId, int itemId)
|
|
{
|
|
ncAdapter.Connect();
|
|
|
|
CmsError libraryError = ncAdapter.RemoveFromQueue(processId, itemId);
|
|
if (libraryError.IsError())
|
|
return BadRequest(libraryError.localizationKey);
|
|
|
|
return Ok();
|
|
}
|
|
|
|
[Route("queue/{processId:int}/move"), HttpPut]
|
|
public IHttpActionResult MoveQueueItems(int processId, MoveItems itemsPositions)
|
|
{
|
|
CmsError libraryError = ncAdapter.MoveQueueItems(processId, itemsPositions.ObjectId, itemsPositions.NewPosition, out List<DTOQueueModel> queue);
|
|
if (libraryError.IsError())
|
|
return BadRequest(libraryError.localizationKey);
|
|
|
|
return Ok(queue);
|
|
}
|
|
|
|
[Route("queue/{processId:int}/edit/{itemId:int}"), HttpPut]
|
|
public IHttpActionResult EditQueueItem(int processId, int itemId, RepsModel reps)
|
|
{
|
|
if (reps.Reps < 1)
|
|
return BadRequest(INCORRECT_PARAMETERS_ERROR.localizationKey);
|
|
|
|
CmsError libraryError = ncAdapter.EditQueueItemReps(processId, itemId, reps.Reps, out DTOQueueModel queueItem);
|
|
if (libraryError.IsError())
|
|
return BadRequest(libraryError.localizationKey);
|
|
|
|
return Ok(queueItem);
|
|
}
|
|
|
|
[Route("queue/{processId:int}/empty"), HttpDelete]
|
|
public IHttpActionResult EmptyQueue(int processId)
|
|
{
|
|
CmsError libraryError = ncAdapter.EmptyQueue(processId);
|
|
if (libraryError.IsError())
|
|
return BadRequest(libraryError.localizationKey);
|
|
|
|
return Ok();
|
|
}
|
|
|
|
[Route("queue/start")]
|
|
public IHttpActionResult StartWorkingQueue(int processId)
|
|
{
|
|
ncAdapter.Connect();
|
|
|
|
CmsError libraryError = ncAdapter.StartWorkingQueue(processId);
|
|
if (libraryError.IsError())
|
|
return BadRequest(libraryError.localizationKey);
|
|
|
|
return Ok();
|
|
}
|
|
|
|
[Route("queue/stop")]
|
|
public IHttpActionResult StopWorkingQueue(int processId)
|
|
{
|
|
ncAdapter.Connect();
|
|
|
|
CmsError libraryError = ncAdapter.StopWorkingQueue(processId);
|
|
if (libraryError.IsError())
|
|
return BadRequest(libraryError.localizationKey);
|
|
|
|
return Ok();
|
|
}
|
|
|
|
[Route("macros")]
|
|
public IHttpActionResult GetMacros()
|
|
{
|
|
return Ok(MacrosConfig);
|
|
}
|
|
|
|
public class MoveItems
|
|
{
|
|
public int ObjectId;
|
|
|
|
public int NewPosition;
|
|
}
|
|
|
|
public class RepsModel
|
|
{
|
|
public int Reps;
|
|
}
|
|
|
|
#endregion Queue Manager
|
|
}
|
|
} |