private void CacheValidResponse(System.Web.HttpContext context, StorageFile file)
        {
            context.Response.Clear();
            context.Response.ClearHeaders();
            context.Response.ClearContent();

            context.Response.StatusCode = 304;
            context.Response.SuppressContent = true;
        }
Exemple #2
0
        public IList<StorageFile> Decompress()
        {
            IList<StorageFile> results = new List<StorageFile>();

            ZipFile file;
            if (zipstream == null)
                file = ZipFile.Read(compressedFile);
            else
                file = ZipFile.Read(zipstream);

            using (file)
            {
                file.FlattenFoldersOnExtract = true;
                foreach (ZipEntry entry in file)
                {
                    StorageFile result = new StorageFile();

                    //flatten all directories, since we don't currently support directories
                    int pos = entry.FileName.LastIndexOf("/");
                    if (pos >= 0)
                        result.Filename = entry.FileName.Substring(pos + 1);
                    else
                        result.Filename = entry.FileName;

                    using (MemoryStream ms = new MemoryStream())
                    {
                        entry.Extract(ms);
                        result.Data = ms.ToArray();
                    }

                    results.Add(result);
                }
            }

            return results;
        }
        private void GenerateThumbnail(StorageFile file, IList<StorageFile> results)
        {
            if (file.Data.Length > 0)
            {
                using (MemoryStream stream = new MemoryStream(file.Data))
                {
                    using (MemoryStream output = new MemoryStream())
                    {
                        try
                        {
                            KalikoImage image = new KalikoImage(stream);
                            int width = 60;
                            int height = 60;

                            image.BackgroundColor = Color.White;
                            KalikoImage thumb = image.GetThumbnailImage(width, height, ThumbnailMethod.Pad);
                            thumb.SaveJpg(output, 75);

                            FileInfo info = new FileInfo(file.Filename);

                            StorageFile thumbnail = new StorageFile();
                            thumbnail.Filename = info.Name.Replace(info.Extension, "") + "-thumb.jpg";
                            thumbnail.Data = output.ToArray();

                            results.Add(thumbnail);
                        }
                        catch (Exception)
                        {
                            throw new ArgumentException("The uploaded content is not supported. Must be a valid image type (png,jpeg or gif)");
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Adds a new image to the site
        /// </summary>
        /// <param name="siteGuid"></param>
        /// <param name="contents"></param>
        public IList<StorageFile> AddImage(Data.Guid siteGuid, String folder, String filename, String contentType, byte[] contents)
        {
            IList<StorageFile> images = new List<StorageFile>();
            if (filename.ToLower().EndsWith("zip"))
            {
                ZipHandler handler = new ZipHandler(contents);
                images = handler.Decompress();

                //Make sure that the images are valid, removing any invalid ones
                for (int i = images.Count - 1; i != 0; i--)
                {
                    StorageFile file = images[i];
                    if (!IsValidImageType(file.Filename))
                        images.RemoveAt(i);
                }
            }
            else
            {
                if (!IsValidImageType(filename))
                    throw new ArgumentException("The specified image is not a supported image type. The filename must end with one of the approved extensions: " + ValidImageExtensions.AsString(",") + ". Filename:" + filename);
                StorageFile file = new StorageFile();
                file.Filename = filename;
                file.Data = contents;
                file.ContentType = contentType;
                images.Add(file);
            }

            IStorageClient client = StorageHelper.GetStorageClient();
            String container = SiteHelper.GetStorageKey(SiteHelper.ImagesContainerKey, siteGuid.Value);
            foreach (StorageFile file in images)
            {
                if ((file.Filename.Length > 0) && (file.Data.Length > 0))
                {
                    client.Save(container, folder, file.Filename, file.Data, Permissions.Public);
                }
            }

            IList<StorageFile> results = new List<StorageFile>();

            //Add any new images to the database
            SaveImagesToDatabase(siteGuid, client, container, folder, images, results);

            return results;
        }