public async Task<MediaFile> UploadMediaAsync(MediaUpload upload) { var blobId = await GetNewBlobIdAsync(upload.ContentType); var url = string.Format("{0}/content/{1}", BaseApiUrl, blobId); using (var client = new HttpClient()) { var response = await client.PostAsync(url, upload.Content); if (!response.IsSuccessStatusCode) return null; } var media = new MediaFile { BlobId = blobId, ContentSize = upload.ContentSize, ContentType = upload.ContentType, Uploaded = DateTime.UtcNow }; var inserted = _mediaRepository.Insert(media); _unit.Commit(); Log.InfoFormat("Uploaded media to FooCDN. Id={0} BlobId={1} ContentSize={2} ContentType={3} StorageType={4}", media.Id, media.BlobId, media.ContentSize, media.ContentType, media.StorageType); return inserted; }
public async Task<ActionResult> Create(EventInformation model) { var profile = _profileService.GetProfile(User.Identity.GetUserId().ToLong()); if (!ModelState.IsValid) { ModelState.AddModelError("", "All of the form fields are required"); var createModel = new CreateEventViewModel(profile); return View(createModel); } var start = new DateTime(model.StartDate.Year, model.StartDate.Month, model.StartDate.Day, model.StartTime.Hour, model.StartTime.Minute, model.StartTime.Second); var end = new DateTime(model.EndDate.Year, model.EndDate.Month, model.EndDate.Day, model.EndTime.Hour, model.EndTime.Minute, model.EndTime.Second); var ev = new Event { DateCreated = DateTime.UtcNow, StartTime = start, EndTime = end, Name = model.EventName, Description = model.Description, Privacy = model.PrivacyLevel, Owner = profile.User, Location = new Location { Name = model.LocationInput, Address = new Address { AddressLine1 = string.Format("{0} {1}", model.street_number, model.route), City = model.locality, CountryRegion = model.country, PostalCode = model.postal_code, StateProvince = model.state } } }; if (!model.lng.IsEmpty() && !model.lat.IsEmpty()) { ev.Location.Geography = DbGeography.FromText(string.Format("POINT ({0} {1})", model.lng, model.lat)); } if (model.Picture != null) { using (var content = new MultipartContent()) { content.Add(new StreamContent(model.Picture.InputStream)); var upload = new MediaUpload { Content = content, ContentSize = model.Picture.ContentLength, ContentType = model.Picture.ContentType }; var picture = await _mediaService.UploadMediaAsync(upload); ev.Picture = picture; Log.InfoFormat("Uploaded picture for event. MediaId={0}", picture.Id); } } // If they want to create on facebook, do it here try { if (model.CreateFacebookEvent) { var fbEvent = new FacebookEvent { Name = ev.Name, Description = ev.Description, State_Time = start, Owner = new FacebookIdentity { Id = profile.FacebookId } }; var eventId = await _facebookService.PublishEvent(profile.User, fbEvent); Log.InfoFormat("Created Facebook Event for new event. UserId={0} FacebookId={1}", profile.User.Id, eventId); ev.FacebookId = eventId; } } catch (Exception e) { Log.ErrorFormat("Failed to create Facebook Event for new event. UserId={0}", profile.User.Id); } // Add yourself to the invitees list ev.Invitations = new List<Invitation> { new Invitation { FacebookUserId = profile.FacebookId, FacebookName = profile.GetFullName(), Response = RSVP.YES } }; // Save event _eventService.CreateEvent(ev); Log.InfoFormat("Created new Event. UserId={0} EventId={1}", profile.User.Id, ev.Id); return RedirectToAction("Event", new {Id = ev.Id}); }
public async Task<ActionResult> UploadMedia(HttpPostedFileBase file, long eventId) { if (file == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "You must provide a file"); var validExtensions = new[] {".jpg", ".jpeg", ".png", ".gif", ".mp4"}; var isValid = validExtensions.Any(ext => file.FileName.EndsWith(ext, true, CultureInfo.InvariantCulture)); if (!isValid) return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "You must provide a file of type jpg, png, gif, or mp4"); var ev = _eventService.GetEvent(eventId); using (var content = new MultipartContent()) { content.Add(new StreamContent(file.InputStream)); var upload = new MediaUpload { Content = content, ContentSize = file.ContentLength, ContentType = file.ContentType }; var picture = await _mediaService.UploadMediaAsync(upload); _eventService.AddMediaToEvent(ev, picture); return Json(new { Url = picture.Url }); } }
private async Task<MediaFile> SaveImage(MultipartFileData file) { var path = file.LocalFileName; var fileStream = new FileStream(path, FileMode.Open); using (var content = new MultipartContent()) { content.Add(new StreamContent(fileStream)); var upload = new MediaUpload { Content = content, ContentSize = fileStream.Length, ContentType = "image/jpeg" }; var picture = await _mediaService.UploadMediaAsync(upload); return picture; } }