예제 #1
0
        private void DownloadThumbnailDone(String fileId)
        {
            log.Debug("[DownloadThumbnailDone] Download done - fileDescriptorId:[{0}]", fileId);

            if (!thumbnailDownloaded.Contains(fileId))
            {
                thumbnailDownloaded.Add(fileId);
            }

            //If it's a direct download we need to create thumbnail
            if (thumbnailDirectToDownload.Contains(fileId))
            {
                log.Debug("[DownloadThumbnailDone] Direct Download done - fileDescriptorId:[{0}]", fileId);

                // Need to scale thumbnail according density and max size expected
                double density   = 1;
                double maxWidth  = MaxThumbnailWidth * density;
                double maxHeight = MaxThumbnailHeight * density;

                String filePath          = GetFullFilePath(fileId);
                String filePathThumbnail = GetThumbnailFullFilePath(fileId);
                try
                {
                    using (Stream stream = new MemoryStream(File.ReadAllBytes(filePath)))
                    {
                        System.Drawing.Size size = avatarPool.GetSize(stream);

                        double width  = size.Width * density;
                        double height = size.Height * density;

                        double scaleWidth  = density;
                        double scaleHeight = density;
                        double scaleUsed;

                        if (width > maxWidth)
                        {
                            scaleWidth = maxWidth / size.Width;
                        }

                        if (height > maxHeight)
                        {
                            scaleHeight = maxHeight / size.Height;
                        }

                        if (scaleWidth > scaleHeight)
                        {
                            scaleUsed = scaleHeight;
                        }
                        else
                        {
                            scaleUsed = scaleWidth;
                        }

                        // Scale thumbnail
                        int widthUsed  = (int)Math.Floor(size.Width * scaleUsed);
                        int heightUsed = (int)Math.Floor(size.Height * scaleUsed);

                        stream.Seek(0, SeekOrigin.Begin);

                        Stream resultStream = avatarPool.GetScaled(stream, widthUsed, heightUsed);

                        // Delete previous thumbnail file (if any ... Whould not occurs)
                        if (File.Exists(filePathThumbnail))
                        {
                            File.Delete(filePathThumbnail);
                        }

                        // Save new image
                        using (Stream streamFile = File.Create(filePathThumbnail))
                            resultStream.CopyTo(streamFile);

                        resultStream.Close();

                        log.Debug("[FileStorage_FileDownloadUpdated] scale thumbnail according density - fileDescriptorId:[{0}] - density:[{1}]", fileId, density);
                    }
                }
                catch
                {
                }
            }
            else
            {
                log.Debug("[FileStorage_FileDownloadUpdated] Thumbnail Download done - fileDescriptorId:[{0}]", fileId);
            }

            Task task = new Task(() =>
            {
                ThumbnailAvailable.Invoke(this, new IdEventArgs(fileId));
            });

            task.Start();
        }
예제 #2
0
 internal void OnThumbnailAvailable(object sender, IdEventArgs args)
 {
     ThumbnailAvailable.Raise(sender, args);
 }