Exemple #1
0
        private void DeleteOldFile(
            Property property,
            ImageSettings setting,
            IDictionary <string, object> recordDict)
        {
            if (recordDict.ContainsKey(property.Column.Undecorate()))
            {
                var fileName = recordDict[property.Column.Undecorate()].ToStringSafe();
                var path     = Pather.Combine(
                    BasePath,
                    property.FileOptions.Path,
                    setting.SubPath,
                    fileName);

                _deleter.Delete(path);
            }
        }
Exemple #2
0
        public void Delete(IEnumerable <PropertyValue> propertiesValues)
        {
            foreach (var propertyValue in propertiesValues)
            {
                var settings = propertyValue.Property.FileOptions.Settings.ToList();
                if (propertyValue.Property.TypeInfo.IsFile)
                {
                    settings = settings.Take(1).ToList();
                }

                foreach (var setting in settings)
                {
                    var fileName = propertyValue.AsString;
                    var path     = Pather.Combine(
                        BasePath,
                        propertyValue.Property.FileOptions.Path,
                        setting.SubPath,
                        fileName);

                    _deleter.Delete(path);
                }
            }
        }
Exemple #3
0
        public void DeleteUploaded(IEnumerable <PropertyValue> propertiesValues)
        {
            foreach (var propertyValue in propertiesValues)
            {
                var settings = propertyValue.Property.FileOptions.Settings.ToList();
                if (propertyValue.Property.TypeInfo.IsFile)
                {
                    settings = settings.Take(1).ToList();
                }

                foreach (var setting in settings)
                {
                    var fileName = propertyValue.AsString;
                    var path     = Pather.Combine(
                        BasePath,
                        propertyValue.Property.FileOptions.Path,
                        Pather.Join(setting.SubPath, _configuration.UploadFilesTempFolderSufix),
                        fileName);

                    _deleter.Delete(path);
                }
            }
        }
Exemple #4
0
        public void ProcessUploaded(
            IEnumerable <PropertyValue> propertiesValues,
            IDictionary <string, object> existingRecord = null)
        {
            foreach (var propertyValue in propertiesValues)
            {
                var settings = propertyValue.Property.FileOptions.Settings.ToList();
                if (propertyValue.Property.TypeInfo.IsImage == false)
                {
                    settings = settings.Take(1).ToList();
                }

                foreach (var setting in settings)
                {
                    DeleteOldFile(propertyValue.Property, setting, existingRecord);

                    var fileName = propertyValue.AsString;
                    if (fileName.IsNullOrEmpty() == false)
                    {
                        var sourcePath = Pather.Combine(
                            BasePath,
                            propertyValue.Property.FileOptions.Path,
                            Pather.Join(setting.SubPath, _configuration.UploadFilesTempFolderSufix),
                            fileName);

                        var targetPath = Pather.Combine(
                            BasePath,
                            propertyValue.Property.FileOptions.Path,
                            setting.SubPath,
                            fileName);

                        System.IO.File.Move(sourcePath, targetPath);
                    }
                }
            }
        }
Exemple #5
0
        public IEnumerable <PropertyValue> Upload(
            EntityRecord entityRecord,
            Func <Property, object> defaultValueResolver)
        {
            var proccessedProperties = new List <PropertyValue>();

            foreach (var propertyValue in entityRecord.Values
                     .Where(value => value.Property.TypeInfo.IsFile))
            {
                if (propertyValue.DataBehavior == DataBehavior.Clear)
                {
                    propertyValue.Raw = defaultValueResolver(propertyValue.Property);
                    proccessedProperties.Add(propertyValue);
                    continue;
                }
                var file = (HttpPostedFileWrapper)propertyValue.Raw;
                if (file == null || file.ContentLength == 0)
                {
                    propertyValue.DataBehavior = DataBehavior.Skip;
                    continue;
                }

                if (propertyValue.Property.TypeInfo.IsFileStoredInDb)
                {
                    var setting         = propertyValue.Property.FileOptions.Settings.FirstOrDefault();
                    var fileInputStream = propertyValue.Property.TypeInfo.IsImage ?
                                          _resizer.Resize(file.InputStream, setting.Width, setting.Height) :
                                          file.InputStream;

                    var bytes = _saver.GetFileByteArray(fileInputStream);
                    propertyValue.Raw = bytes;
                }
                else
                {
                    var fileName = _fileNameCreator.GetFileName(propertyValue.Property, file);
                    propertyValue.Raw = fileName;

                    if (propertyValue.Property.TypeInfo.IsImage)
                    {
                        foreach (var setting in propertyValue.Property.FileOptions.Settings)
                        {
                            var resizedStream = _resizer.Resize(
                                file.InputStream,
                                setting.Width,
                                setting.Height);

                            var path = Pather.Combine(
                                BasePath,
                                propertyValue.Property.FileOptions.Path,
                                Pather.Join(setting.SubPath, _configuration.UploadFilesTempFolderSufix),
                                fileName);

                            _saver.SaveFile(resizedStream, path);
                            resizedStream.Dispose();
                        }
                    }
                    else
                    {
                        var path = Pather.Combine(
                            BasePath,
                            Pather.Join(
                                propertyValue.Property.FileOptions.Path,
                                _configuration.UploadFilesTempFolderSufix),
                            fileName);

                        _saver.SaveFile(file.InputStream, path);
                    }

                    proccessedProperties.Add(propertyValue);
                }

                file.InputStream.Dispose();
            }

            return(proccessedProperties);
        }