Пример #1
0
        public Tuple <ErrorResponse, Template> ProcessTemplateFile(IFormFile file)
        {
            var folderName = Path.Combine("Resources", "File");
            var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);

            if (file.Length > 0)
            {
                var retVal = new Template();
                retVal.Id = repository.GenerateTemplateId();
                var fileName = $"{retVal.Id}";
                var fullPath = Path.Combine(pathToSave, fileName);
                var dbPath   = Path.Combine(folderName, fileName);

                using (var stream = new FileStream(fullPath, FileMode.Create))
                {
                    file.CopyTo(stream);
                }
                var defaultSensor  = enumRepository.GetValues <Sensor>()[0];
                var legacyTemplate = ConvertXMLFileToXmlTemplate(fullPath);
                retVal.Version = legacyTemplate.GroupNames.TemplateGroups.TemplateGroupRow.Template.Version;
                retVal.TemplateInputMapping = new List <TemplateInputMapping>();
                retVal.Name = legacyTemplate.GroupNames.Title;
                foreach (var inputOutput in legacyTemplate.GroupNames.TemplateInputOutput.IO)
                {
                    var fileTypeData = legacyTemplate.GroupNames.FileInfoTable.Row.Find(_ => _.FileTypeID == inputOutput.FileTypeID);
                    if (fileTypeData == null)
                    {
                        return(new Tuple <ErrorResponse, Template>(new ErrorResponse(400.4, $"Unkown file type for input:{inputOutput.Title}."), null));
                    }
                    var fileType = new FileType
                    {
                        FileTypeId      = int.Parse(fileTypeData.FileTypeID),
                        DisplayName     = fileTypeData.Title,
                        DefaultFileName = fileTypeData.matchfilename,
                        ExtensionName   = fileTypeData.Extensions,
                        IsHeader        = bool.Parse(fileTypeData.IsHeader),
                        IsImage         = bool.Parse(fileTypeData.IsImage),
                        IsRaw           = bool.Parse(fileTypeData.IsRaw),
                        IsXML           = bool.Parse(fileTypeData.IsXML)
                    };

                    retVal.TemplateInputMapping.Add(new TemplateInputMapping
                    {
                        id         = repository.GenerateTemplateId(),
                        FieldName  = inputOutput.FieldName,
                        FileType   = fileType,
                        IsInput    = inputOutput.Direction == "Input",
                        moduleName = inputOutput.ModuleName,
                        Sensor     = defaultSensor
                    });
                }
                return(new Tuple <ErrorResponse, Template>(null, retVal));
            }
            else
            {
                return(new Tuple <ErrorResponse, Template>(new ErrorResponse(400.3, "The Template input upload should contain a file"), null));
            }
        }