Esempio n. 1
0
        public void OnImageAttachmentCompleted(ImageAttachmentEventArgs evt)
        {
            EventHandler <ImageAttachmentEventArgs> handler = ImageAttachmentCompleted;

            if (handler != null)
            {
                handler(this, evt);
            }
        }
        public void HandleImageAttachmentCompleted(object sender, ImageAttachmentEventArgs evt)
        {
            if (evt.Attachment.type != AttachmentType.image || evt.Meta != ImageMeta.Origin)
            {
                return;
            }


            TaskQueue.EnqueueMedium(this.HandleImageAttachmentCompletedSync, evt);
        }
Esempio n. 3
0
        public void Execute(Attachment file, ImageMeta meta, NameValueCollection Parameters, Driver driver, string savedName, AttachmentUploadHandler handler)
        {
            this.handler = handler;
            this.driver  = driver;


            file.file_size       = file.RawData.Count;
            file.modify_time     = DateTime.UtcNow;
            file.url             = "/v2/attachments/view/?object_id=" + file.object_id;
            file.saved_file_name = savedName;

            BeforeSaveAttachment(file, Parameters, meta);
            new FileStorage(driver).SaveFile(savedName, file.RawData);
            SaveAttachmentInfoToDB(file, meta, savedName);

            AttachmentEventArgs aEvtArgs = new AttachmentEventArgs
            {
                Attachment = file,
                Driver     = driver
            };

            ImageAttachmentEventArgs evtArgs = new ImageAttachmentEventArgs
            {
                Attachment       = file,
                Meta             = meta,
                UserApiKey       = Parameters["apikey"],
                UserSessionToken = Parameters["session_token"],
                Driver           = driver
            };

            handler.OnAttachmentSaved(aEvtArgs);
            handler.OnImageAttachmentSaved(evtArgs);

            HttpHelper.RespondSuccess(handler.Response, ObjectUploadResponse.CreateSuccess(file.object_id));

            handler.OnImageAttachmentCompleted(evtArgs);
        }
        public void HandleImageAttachmentSaved(object sender, ImageAttachmentEventArgs evt)
        {
            System.Diagnostics.Debug.Assert(evt.Attachment.type == AttachmentType.image);
            if (evt.Meta != ImageMeta.Origin)
            {
                return;
            }

            if (evt.Attachment.image_meta != null && evt.Attachment.image_meta.medium != null)
            {
                return;
            }

            try
            {
                ThumbnailInfo medium;
                Attachment    update;

                using (Bitmap origImage = BuildBitmap(evt.Attachment.RawData))
                {
                    // release raw data immediately
                    evt.Attachment.RawData = new ArraySegment <byte>();
                    medium = MakeThumbnail(origImage, ImageMeta.Medium,
                                           evt.Attachment.Orientation,
                                           evt.Attachment.object_id,
                                           evt.Driver,
                                           evt.Attachment.file_name);

                    update = new Attachment
                    {
                        object_id  = evt.Attachment.object_id,
                        image_meta = new ImageProperty
                        {
                            width  = origImage.Width,
                            height = origImage.Height,
                            medium = medium
                        }
                    };
                }

                BsonDocument exist = AttachmentCollection.Instance.FindOneAs <BsonDocument>(
                    Query.EQ("_id", evt.Attachment.object_id));
                exist.DeepMerge(update.ToBsonDocument());
                AttachmentCollection.Instance.Save(exist);

                TaskQueue.EnqueueMedium(this.UpstreamThumbnail,
                                        new UpstreamArgs
                {
                    FullImageId      = evt.Attachment.object_id,
                    GroupId          = evt.Attachment.group_id,
                    ImageMeta        = ImageMeta.Medium,
                    Thumbnail        = medium,
                    UserApiKey       = evt.UserApiKey,
                    UserSessionToken = evt.UserSessionToken
                });
            }
            catch (Exception e)
            {
                logger.Warn("Unabel to make thumbnail and upstream", e);
            }
        }
        public void HandleImageAttachmentCompletedSync(object args)
        {
            ImageAttachmentEventArgs evt = (ImageAttachmentEventArgs)args;

            if (evt.Attachment.type != AttachmentType.image || evt.Meta != ImageMeta.Origin)
            {
                return;
            }

            try
            {
                ThumbnailInfo small;
                ThumbnailInfo large;
                ThumbnailInfo square;
                string        origImgObjectId = evt.Attachment.object_id;

                using (Bitmap origImage = BuildBitmap(evt.Attachment.RawData))
                {
                    // release raw data immediately
                    evt.Attachment.RawData = new ArraySegment <byte>();

                    small = MakeThumbnail(origImage, ImageMeta.Small, evt.Attachment.Orientation,
                                          origImgObjectId, evt.Driver, evt.Attachment.file_name);
                    large = MakeThumbnail(origImage, ImageMeta.Large, evt.Attachment.Orientation,
                                          origImgObjectId, evt.Driver, evt.Attachment.file_name);
                    square = MakeThumbnail(origImage, ImageMeta.Square, evt.Attachment.Orientation,
                                           origImgObjectId, evt.Driver, evt.Attachment.file_name);
                }

                Attachment update = new Attachment
                {
                    object_id  = evt.Attachment.object_id,
                    image_meta = new ImageProperty
                    {
                        small  = small,
                        large  = large,
                        square = square
                    }
                };

                BsonDocument doc = AttachmentCollection.Instance.FindOneAs <BsonDocument>(
                    Query.EQ("_id", origImgObjectId));
                doc.DeepMerge(update.ToBsonDocument());
                AttachmentCollection.Instance.Save(doc);
                doc.Clear();
                doc = null;

                UpstreamThumbnail(small, evt.Attachment.group_id, evt.Attachment.object_id,
                                  ImageMeta.Small, evt.UserApiKey, evt.UserSessionToken);
                UpstreamThumbnail(large, evt.Attachment.group_id, evt.Attachment.object_id,
                                  ImageMeta.Large, evt.UserApiKey, evt.UserSessionToken);
                UpstreamThumbnail(square, evt.Attachment.group_id, evt.Attachment.object_id,
                                  ImageMeta.Square, evt.UserApiKey, evt.UserSessionToken);


                long newValue = Interlocked.Add(ref g_counter, 1L);
                if (newValue % 5 == 0)
                {
                    GC.Collect();
                }
            }
            catch (Exception e)
            {
                logger.Warn("Image attachment post processing unsuccess", e);
            }
        }