Exemplo n.º 1
0
        /// <summary>
        /// Upload files to temp location, or save file byte array to property value
        /// </summary>
        public IList <Property> Upload(Entity entity)
        {
            var proccessedProperties = new List <Property>();

            foreach (var property in entity
                     .CreateProperties(getForeignCollection: false)
                     .Where(x => x.TypeInfo.IsFile))
            {
                if (property.Value.Raw.IsBehavior(DataBehavior.Clear))
                {
                    property.Value.Raw = property.Value.DefaultValue;
                    proccessedProperties.Add(property);
                    continue;
                }
                var file = (HttpPostedFileWrapper)property.Value.Raw;
                if (file == null || file.ContentLength == 0)
                {
                    property.Value.Raw = DataBehavior.Skip;
                    continue;
                }

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

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

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

                            var subPath =
                                setting.SubPath.TrimEnd('/', '\\') +
                                _configuration.UploadFilesTempFolderSufix;
                            var path = Path.Combine(BasePath, property.FileOptions.Path, subPath, fileName);

                            _saver.SaveFile(resizedStream, path);
                            resizedStream.Dispose();
                        }
                    }
                    else
                    {
                        var subPath =
                            property.FileOptions.Path.TrimEnd('/', '\\') +
                            _configuration.UploadFilesTempFolderSufix;
                        var path = Path.Combine(BasePath, subPath, fileName);

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

                    proccessedProperties.Add(property);
                }

                file.InputStream.Dispose();
            }

            return(proccessedProperties);
        }
Exemplo n.º 2
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);
        }