コード例 #1
0
        public ActionResult AddImage(int id, HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                // TODO add thumbnail generation - probably a separate class, after
                Gallery       gallery       = galleriesRepository.Galleries.Find(id);
                GalleryFolder galleryFolder = new GalleryFolder(gallery.Path);
                galleryFolder.AddFile(file);
            }

            return(RedirectToAction("Edit", new { id }));
        }
コード例 #2
0
        private static async Task <GalleryFolder> DeleteMediaInternalAsync(Guid id, ILogger log)
        {
            log.LogInformation("C# HTTP delete media trigger function processed a request.");
            var storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage"));
            var blobClient     = storageAccount.CreateCloudBlobClient();
            var container      = blobClient.GetContainerReference("images");

            JsonSerializerOptions options = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true,
                PropertyNamingPolicy        = JsonNamingPolicy.CamelCase
            };

            GalleryFolder root = new GalleryFolder();
            // load images.json
            var blob = container.GetBlockBlobReference(imagesJsonFilename);

            if (blob != null)
            {
                if (await blob.ExistsAsync())
                {
                    var body = await blob.DownloadTextAsync();

                    if (!string.IsNullOrWhiteSpace(body))
                    {
                        root = JsonSerializer.Deserialize <GalleryFolder>(body, options);
                    }
                }

                // find folder
                var folder = FindImageFolder(root, id);
                if (folder != null)
                {
                    var item = folder.Images.SingleOrDefault(x => x.Id == id);
                    if (item != null)
                    {
                        folder.Images.Remove(item);
                        var outputBody = JsonSerializer.Serialize(root, options);
                        await blob.UploadTextAsync(outputBody);

                        log.LogInformation("Updated images.json");
                        foreach (var key in item.Files.Keys)
                        {
                            var path = item.Files[key].Path;
                            log.LogInformation("Deleted {deleted}", path);
                            var f = container.GetBlockBlobReference(path);
                            await f.DeleteIfExistsAsync();
                        }
                    }
                }
            }
            return(root);
        }
コード例 #3
0
 private static GalleryFolder FindImageFolder(GalleryFolder root, Guid id)
 {
     if (root.Images.Any(x => x.Id == id))
     {
         return(root);
     }
     foreach (var f in root.Folders)
     {
         var r = FindImageFolder(f, id);
         if (r != null)
         {
             return(r);
         }
     }
     return(null);
 }
コード例 #4
0
 private static GalleryFolder GetFolderRecursive(GalleryFolder root, string id)
 {
     if (root.Id == id)
     {
         return(root);
     }
     foreach (var f in root.Folders)
     {
         var r = GetFolderRecursive(f, id);
         if (r != null)
         {
             return(r);
         }
     }
     return(null);
 }
コード例 #5
0
        public ActionResult Create(GalleryNew galleryNew)
        {
            if (ModelState.IsValid)
            {
                GalleryFolder galleryFolder = new GalleryFolder(galleryNew.Title);
                string        galleryPath   = galleryFolder.CreateGalleryDirectories();


                Gallery gallery = new Gallery {
                    Path = galleryPath
                };
                gallery.InjectFrom(galleryNew);
                galleriesRepository.Galleries.Add(gallery);
                galleriesRepository.SaveChanges();

                return(RedirectToAction("Index"));
            }

            return(View(galleryNew));
        }
コード例 #6
0
        public ActionResult DeleteGallery(int id)
        {
            if (ModelState.IsValid)
            {
                Gallery galleryToDelete = galleriesRepository.Galleries.Find(id);
                galleriesRepository.Galleries.Remove(galleryToDelete);
                galleriesRepository.SaveChanges();

                try
                {
                    GalleryFolder galleryFolder = new GalleryFolder(galleryToDelete.Path);
                    galleryFolder.DeleteGalleryFolders();
                }
                catch
                {
                }

                return(RedirectToAction("Index"));
            }

            return(View("Index"));
        }
コード例 #7
0
        private static async Task <GalleryFolder> ProcessMediaInternalAsync(ILogger log, string name, string folder, string description)
        {
            log.LogInformation("C# HTTP process media trigger function processed a request.");

            var storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage"));
            var blobClient     = storageAccount.CreateCloudBlobClient();

            var srcContainer = blobClient.GetContainerReference("image-upload");
            var container    = blobClient.GetContainerReference("images");

            JsonSerializerOptions options = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true,
                PropertyNamingPolicy        = JsonNamingPolicy.CamelCase
            };

            GalleryFolder root = new GalleryFolder()
            {
                Id = Guid.Empty.ToString()
            };

            // load images.json
            var blob = container.GetBlockBlobReference(imagesJsonFilename);

            if (blob != null)
            {
                if (await blob.ExistsAsync())
                {
                    var body = await blob.DownloadTextAsync();

                    if (!string.IsNullOrWhiteSpace(body))
                    {
                        root = JsonSerializer.Deserialize <GalleryFolder>(body, options);
                    }
                }

                // load file
                var f = srcContainer.GetBlockBlobReference(name);
                using MemoryStream stream = new MemoryStream();
                await f.DownloadToStreamAsync(stream).ConfigureAwait(true);

                stream.Position = 0;
                using var src   = Image.Load(stream);

                var id           = Guid.NewGuid();
                var hueCalc      = new DominantHueColorCalculator(0.5f, 0.5f, 60);
                var galleryImage = new GalleryImage()
                {
                    Id             = id,
                    DominantColour = "#" + hueCalc.CalculateDominantColor(src).ToHex(),
                    Files          = new Dictionary <string, GalleryImageDetails>(),
                    Description    = description
                };

                var resolutions = GetResolutions();
                foreach (var key in resolutions.Keys)
                {
                    galleryImage.Files[key] = await CreatePreviewImageAsync(
                        src.Clone(x => x.AutoOrient()),
                        key,
                        container,
                        id,
                        resolutions
                        );
                }

                if (!string.IsNullOrWhiteSpace(folder))
                {
                    (GetFolderRecursive(root, folder) ?? root)
                    .Images.Add(galleryImage);
                }
                else
                {
                    root.Images.Add(galleryImage);
                }
                // save images.json
                var outputBody = JsonSerializer.Serialize(root, options);
                await blob.UploadTextAsync(outputBody);

                log.LogInformation("Updated images.json");

                // delete original file
                await f.DeleteAsync().ConfigureAwait(true);
            }
            return(root);
        }