示例#1
0
        public static IEnumerable<Model.Picture> RersizeTo(Repository.Model.File fromFile,
            Repository.Logic.Repository repository,
            IFileStorage storage,
            Model.PictureType[] toType)
        {
            if (fromFile == null)
                throw new ArgumentNullException(nameof(fromFile));
            if (repository == null)
                throw new ArgumentNullException(nameof(repository));
            if (storage == null)
                throw new ArgumentNullException(nameof(storage));

            toType = toType
                ?? typeof(Model.PictureType)
                .GetEnumValues()
                .Cast<Model.PictureType>()
                .ToArray();

            var toSizes = toType
                .Select(i => new { PictureType = i, Size = GetSize(i) })
                .Where(i => i.Size != null)
                .ToArray();

            if (toSizes.Any())
                using (var fileStream = storage.FileGet(fromFile.FileId))
                using (var originalImage = Bitmap.FromStream(fileStream))
                {
                    var pictures = toSizes.Select(t =>
                    {
                        var newWidth = (t.Size.Width == 0) ? originalImage.Width : t.Size.Width;
                        var newHeight = (t.Size.Height == 0) ? originalImage.Height : t.Size.Height;

                        using (var resizedbitmap = ResizeBitmap(originalImage, newWidth, newHeight))
                        using (var newPictureStream = new MemoryStream())
                        {
                            resizedbitmap.Save(newPictureStream, originalImage.RawFormat);
                            newPictureStream.Seek(0, SeekOrigin.Begin);

                            var dbFile = repository.FilePut(storage, newPictureStream, t.PictureType.ToString() + fromFile.FileName);
                            var picture = repository.New<Repository.Model.Picture>();
                            picture.File = dbFile;
                            picture.FileId = dbFile.FileId;
                            picture.Height = newWidth;
                            picture.Width = newHeight;
                            picture.PictureType = (Repository.Model.PictureType)(int)t.PictureType;
                            repository.Add(picture);
                            return AutoMapper.Mapper.Map<Model.Picture>(picture);
                        }
                    }).ToArray();
                    return pictures;
                }
            return Enumerable.Empty<Model.Picture>();
        }
示例#2
0
        public static IEnumerable<string> FileGetLines(this Repository.Logic.Repository repository, IFileStorage storage, Repository.Model.File repositoryFile)
        {
            if (repository == null)
                throw new ArgumentNullException(nameof(repository));
            if (repositoryFile == null)
                throw new ArgumentNullException(nameof(repositoryFile));
            if (storage == null)
                throw new ArgumentNullException(nameof(storage));

            var res = new List<string>();
            using (var fileStream = storage.FileGet(repositoryFile.FileId))
            using (var sr = new System.IO.StreamReader(fileStream, repositoryFile.Encoding))
            {
                var line = string.Empty;
                while ((line = sr.ReadLine()) != null)
                    res.Add(line);
            }
            return res;
        }