示例#1
0
 public async Task <IActionResult> Photos(int albumId)
 {
     using (var proxy = new PhotoProxy())
     {
         ViewData["photos"] = await proxy.GetAsync(albumId);
     }
     return(PartialView());
 }
示例#2
0
 public static Photo ToEntity(this PhotoProxy photo)
 {
     return(new Data.Models.Photo
     {
         Name = photo.Name,
         ImageUrl = photo.ImageUrl,
         PublishedDate = photo.PublishedDate
     });
 }
示例#3
0
        public async Task <HttpResponseMessage> UploadPhoto()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }
            string root         = HttpContext.Current.Server.MapPath("~/" + UPLOAD_DIR);
            string uploadPath   = string.Empty;
            string courseId     = string.Empty;
            string albumId      = string.Empty;
            string userId       = string.Empty;
            string title        = string.Empty;
            var    dummyAlbumId = Guid.NewGuid().ToString();
            var    provider     = new MultipartFormDataStreamProvider(root);

            try
            {
                await Request.Content.ReadAsMultipartAsync(provider);

                CourseDetailProxy course = new CourseDetailProxy();
                foreach (var key in provider.FormData)
                {
                    if (key.Equals("course"))
                    {
                        courseId = JsonConvert.DeserializeObject <string>(provider.FormData[key.ToString()]);
                    }
                    if (key.Equals("album"))
                    {
                        albumId = JsonConvert.DeserializeObject <string>(provider.FormData[key.ToString()]);
                    }
                    if (key.Equals("user"))
                    {
                        userId = JsonConvert.DeserializeObject <string>(provider.FormData[key.ToString()]);
                    }
                    if (key.Equals("name"))
                    {
                        title = JsonConvert.DeserializeObject <string>(provider.FormData[key.ToString()]);
                    }
                }
                albumId    = String.IsNullOrEmpty(albumId) ? dummyAlbumId : albumId;
                uploadPath = root + albumId + @"\";
                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }
                string _newFileName = string.Empty;
                string _path        = string.Empty;
                foreach (MultipartFileData file in provider.FileData)
                {
                    string fileName = file.Headers.ContentDisposition.FileName;
                    if (fileName.StartsWith("\"") && fileName.EndsWith("\""))
                    {
                        fileName = fileName.Trim('"');
                    }
                    if (fileName.Contains(@"/") || fileName.Contains(@"\"))
                    {
                        fileName = Path.GetFileName(fileName);
                    }
                    _newFileName = string.Format("{0}{1}", Guid.NewGuid(), Path.GetExtension(fileName));
                    var moveTo = Path.Combine(uploadPath, _newFileName);
                    if (File.Exists(moveTo))
                    {
                        File.Delete(moveTo);
                    }
                    _path = string.Format("{0}{1}", UPLOAD_DIR + albumId + "/", _newFileName);
                    File.Move(file.LocalFileName, moveTo);
                }
                var photo = new PhotoProxy
                {
                    Name          = title,
                    ImageUrl      = _path,
                    PublishedDate = this._datetimeRepository.Now()
                };
                this._photoAlbumService.AddNewPhoto(courseId, albumId, userId, photo.ToEntity());
                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (System.Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }
        }
示例#4
0
 public NoticeController()
 {
     _proxy      = new NoticeProxy("http://localhost:1101");
     _photoProxy = new PhotoProxy("http://localhost:1101");
 }