Ejemplo n.º 1
0
        public async Task <List <string> > GetImagesFromDiskAsync(int userId, ImageRelationType relationType, int ownerId)
        {
            string contentRootPath = _hostingEnvironment.ContentRootPath;
            var    userPath        = Path.Combine(contentRootPath, "uploads", userId.ToString());
            var    path            = Path.Combine(userPath, relationType.ToString(), ownerId.ToString());

            if (Directory.Exists(path))
            {
                var fileNames = Directory.GetFiles(path, "*.jpg");

                var images = new List <string>();

                foreach (var fileName in fileNames)
                {
                    var      filePath = Path.Combine(path, fileName);
                    FileInfo fileInfo = new FileInfo(filePath);
                    byte[]   image    = new byte[fileInfo.Length];

                    using (FileStream fs = fileInfo.OpenRead())
                    {
                        await fs.ReadAsync(image, 0, image.Length);
                    }

                    images.Add(Convert.ToBase64String(image));
                }

                return(images);
            }

            return(null);
        }
Ejemplo n.º 2
0
        public async Task SaveImagesToDiskAsync(int userId, ImageRelationType relationType, int ownerId, List <UploadImage> images)
        {
            try
            {
                foreach (var image in images)
                {
                    if (image.Image.Length > 0)
                    {
                        var extension = Path.GetExtension(image.FileName);
                        var fileName  = Guid.NewGuid().ToString() + extension;

                        var path = Path.Combine(new[]
                        {
                            userId.ToString(), relationType.ToString(), ownerId.ToString(), fileName
                        });

                        CloudBlockBlob blockBlob = _container.GetBlockBlobReference(path);
                        await blockBlob.UploadFromByteArrayAsync(image.Image, 0, image.Image.Length);
                    }
                }

                _logger.LogInformation("{0} images saved to blob storage. Images count: {1}", relationType.ToString(), images.Count);
            }
            catch (Exception exception)
            {
                _logger.LogError(exception.Message);
            }
        }
Ejemplo n.º 3
0
        public async Task SaveImagesToDiskAsync(int userId, ImageRelationType relationType, int ownerId, List <UploadImage> images)
        {
            string contentRootPath = _hostingEnvironment.ContentRootPath;
            var    userPath        = Path.Combine(contentRootPath, "uploads", userId.ToString());
            var    path            = Path.Combine(userPath, relationType.ToString(), ownerId.ToString());

            Directory.CreateDirectory(path);

            foreach (var image in images)
            {
                if (image.Image.Length > 0)
                {
                    var extension = Path.GetExtension(image.FileName);
                    var fileName  = Guid.NewGuid().ToString() + extension;

                    var filePath = Path.Combine(path, fileName);

                    using (var sourceStream = File.Open(filePath, FileMode.OpenOrCreate))
                    {
                        sourceStream.Seek(0, SeekOrigin.End);
                        await sourceStream.WriteAsync(image.Image, 0, image.Image.Length);
                    }

                    //using (var fileStream = new FileStream(filePath, FileMode.Create))
                    //{
                    //    await image.CopyToAsync(fileStream);
                    //}
                }
            }
        }
Ejemplo n.º 4
0
        public async Task <List <string> > GetImagesFromDiskAsync(int userId, ImageRelationType relationType, int ownerId)
        {
            var images = new List <string>();

            var path = string.Format("{0}/{1}/{2}/", userId, relationType.ToString(), ownerId);

            BlobContinuationToken token = null;

            do
            {
                var directory = _container.GetDirectoryReference(path);

                BlobResultSegment resultSegment = await directory.ListBlobsSegmentedAsync(token);

                token = resultSegment.ContinuationToken;

                foreach (IListBlobItem item in resultSegment.Results)
                {
                    if (item.GetType() == typeof(CloudBlockBlob))
                    {
                        string image;

                        CloudBlockBlob blob = (CloudBlockBlob)item;

                        using (MemoryStream ms = new MemoryStream())
                        {
                            await blob.DownloadToStreamAsync(ms);

                            image = Convert.ToBase64String(ms.ToArray());
                        }

                        images.Add(image);
                    }
                }
            } while (token != null);

            return(images);
        }