예제 #1
0
        public async Task <IHttpActionResult> Upload(string userId, long eventId)
        {
            byte[] picture;
            var    fileName = string.Empty;

            if (string.IsNullOrWhiteSpace(userId) || eventId <= 0)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "The userId and eventId in the URL were invalid"));
            }

            if (Request.Content.IsMimeMultipartContent())
            {
                try
                {
                    var content = (await Request.Content.ReadAsMultipartAsync()).Contents.First();
                    fileName = content.Headers.ContentDisposition.FileName.TrimEnd('"').TrimStart('"');
                    picture  = await content.ReadAsByteArrayAsync();
                }
                catch (Exception e)
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, e));
                }
            }
            else
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
            }

            MediaItem mediaItem = null;

            if (picture.Length == 0)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "The content was empty or unsuccessfully delivered"));
            }

            var    extension = Path.GetExtension(fileName).TrimStart('.');
            string blob      = _fooCDNService.CreateNewBlob(string.Format("image/{0}", extension));

            _fooCDNService.PostToBlob(blob, picture, fileName);

            mediaItem = new MediaItem
            {
                FooCDNBlob = blob,
                Size       = (ulong)picture.LongLength,
                MediaType  = MediaType.Photo
            };

            var post = new Post
            {
                User      = await _userService.FindUserById(userId),
                MediaItem = mediaItem,
                PostText  = string.Empty,
                PostTime  = DateTime.UtcNow,
            };

            _postService.AddOrUpdatePostToEvent(eventId, post);
            _unitOfWork.SaveChanges();

            return(Ok());
        }
예제 #2
0
        public async Task <ActionResult> Post(long id, string text, HttpPostedFileBase picture = null, bool shareOnFacebook = false, bool tweet = false)
        {
            MediaItem mediaItem = null;

            if (picture != null)
            {
                MemoryStream target = new MemoryStream();
                picture.InputStream.CopyTo(target);
                var pictureData = target.ToArray();

                var    extension = Path.GetExtension(picture.FileName).TrimStart('.');
                string blob      = _fooCDNService.CreateNewBlob(string.Format("image/{0}", extension));

                //async
                await _fooCDNService.PostToBlob(blob, pictureData, picture.FileName);

                mediaItem = new MediaItem
                {
                    FooCDNBlob = blob,
                    Size       = (ulong)pictureData.LongLength,
                    MediaType  = MediaType.Photo
                };
            }

            var post = new Post
            {
                User      = await _userService.FindUserById(User.Identity.GetUserId()),
                MediaItem = mediaItem,
                PostText  = text,
                PostTime  = DateTime.Now,
            };

            if (shareOnFacebook)
            {
                AngoraUser user = await _userService.FindUserById(User.Identity.GetUserId());

                string e          = _eventService.FindById(id).Name;
                var    fbUser     = new FacebookClient(user.FacebookAccessToken);
                var    parameters = new Dictionary <string, object> {
                    { "message", "I posted this to the event '" + e + "' on Auderus: \n" + text }
                };

                if (picture != null)
                {
                    parameters.Add("picture", post.MediaItem.GetUrl());
                }

                fbUser.Post("me/feed", parameters);
            }
            _postService.AddOrUpdatePostToEvent(id, post);
            _unitOfWork.SaveChanges();

            return(RedirectToAction("Details", new { id = id }));
        }