Exemplo n.º 1
0
        /// <summary>
        /// Sets or removes a custom material prop value of the text type.
        /// </summary>
        /// <param name="id">The material ID.</param>
        /// <param name="prop">The custom prop ID.</param>
        /// <param name="text">The text to set, or null.</param>
        public void UpdateCustomTextMaterialProp(int id, CustomMaterialProp prop, string text)
        {
            // Make sure the material exists
            GetMaterialOrThrowNotFoundException(id);

            // Proceed
            if (text == null)
            {
                CustomMaterialPropValueRepository.RemoveCustomTextMaterialProp(id, prop.Id);
            }
            else
            {
                CustomMaterialPropValueRepository.SetCustomTextMaterialProp(id, prop.Id, text);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sets or removes a custom material prop value of the file type.
        /// This stores the file in the file system and persists the file path as a custom material prop value.
        /// </summary>
        /// <param name="id">The material ID.</param>
        /// <param name="prop">The custom prop ID.</param>
        /// <param name="file">The file to set, or null.</param>
        public void UpdateCustomFileMaterialProp(int id, CustomMaterialProp prop, IFormFile file)
        {
            // Make sure the material exists
            Material material = GetMaterialOrThrowNotFoundException(id);

            // Get and validate target directory
            string basePath = Path.Combine(Confguration.GetValue <string>("Files:Path"), id.ToString());

            if (!Directory.Exists(basePath))
            {
                Directory.CreateDirectory(basePath);
            }

            if (file == null)
            {
                // Get file path
                CustomMaterialPropValue propValue = material.CustomProps.FirstOrDefault(p => p.PropId == prop.Id);
                string filePath = (string)propValue.Value;

                // Delete file
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
                CustomMaterialPropValueRepository.RemoveCustomFileMaterialProp(id, prop.Id);
            }
            else
            {
                // Write file
                string filePath = Path.Combine(basePath, file.FileName);
                using (FileStream stream = File.Create(filePath))
                {
                    file.CopyTo(stream);
                }
                CustomMaterialPropValueRepository.SetCustomFileMaterialProp(id, prop.Id, filePath);
            }
        }