Esempio n. 1
0
        public JsonResult Upload(UploadAlbumModel m)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    #region Upload

                    if (m.ImageFiles[0] == null)
                        ModelState.AddModelError("", "Bạn phải chọn ít nhất một hình ảnh!");
                    else if (m.ImageFiles.Count > 100)
                        ModelState.AddModelError("", "Bạn chỉ được đăng tối đa 100 ảnh trong một album!");
                    else
                    {
                        var album = new CreateAlbumRequestModel
                            {
                                AlbumContent = m.AlbumContent,
                                AlbumTitle = m.AlbumTitle,
                                CategoryId = m.CategoryId,
                                MemberId = SessionManager.UserLogged.UserId,
                                Tags = m.Tags,
                                ListImage = new List<CreateAlbumRequestModel.ImageRequest>()
                            };

                        foreach (var img in m.ImageFiles)
                        {
                            if (img.ContentType.Contains("image"))
                            {
                                var memoryStream = new MemoryStream();
                                img.InputStream.CopyTo(memoryStream);
                                album.ListImage.Add(new CreateAlbumRequestModel.ImageRequest()
                                    {
                                        ImageFile = Convert.ToBase64String(memoryStream.ToArray()),
                                        ImageType = img.ContentType,
                                        ImageName = img.FileName,
                                        ImageTags = album.Tags,
                                        ImageStory = album.AlbumContent,
                                        ImageTitle = album.AlbumTitle,
                                        DeviceInfo = "web"
                                    });
                            }
                        }
                        var data1 = PostRepository.CreateAlbum(album);

                        if (data1.Code == "-1")
                            ModelState.AddModelError("", data1.Message);
                        else
                            return Json(new { Url = Url.Action("Detail", new { id = data1.Message }), Result = true });
                    }
                    #endregion
                }

                var message = ModelState.Values.Where(item => item.Errors.Count > 0).Aggregate("", (current1, item) => item.Errors.Aggregate(current1, (current, error) => current + (error.ErrorMessage + "</br>")));
                return Json(new {Result = false , Message = message});

            }
            catch (Exception exception)
            {
                Logger.Error(exception.ToString);
                return Json(new { Result = false, Message = exception.Message });
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Create album
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public PostEditResponseModel CreateAlbum(CreateAlbumRequestModel request)
        {
            if (AllowApi)
                if (!CheckApi(new Guid(request.ApiKey)))
                    return new PostEditResponseModel()
                    {
                        Code = "-2",
                        Message = "Not allow this function for this API key. Please check again"
                    };

            var albumId = Guid.NewGuid();

            try
            {
                #region Insert Album

                //Insert to category
                //Get category
                var cat = DataContext.Categories.SingleOrDefault(m => m.CategoryId == new Guid(request.CategoryId));
                if (cat == null)

                    return new PostEditResponseModel
                    {
                        Code = "-1",
                        Message = "Cannot find category"
                    };

                //Insert to relation
                var relation = new Relation()
                {
                    RelationId = Guid.NewGuid(),
                    RelationDataId = albumId,
                    RelationRelatedId = cat.CategoryId,
                    RelationType = "ALBUM_CATEGORY",
                };
                var album = new Album()
                {
                    AlbumId = albumId,
                    AlbumContent = request.AlbumContent,
                    AlbumPermalink = CommonLib.GeneratePermalink(request.AlbumTitle),
                    Tags = request.Tags,
                    AlbumCreateDate = DateTime.UtcNow,
                    AlbumTitle = request.AlbumTitle,
                    MemberId = new Guid(request.MemberId),
                    Status = 1
                };

                #endregion

                #region Insert Image

                var listImage = new List<Image>();

                foreach (var image in request.ListImage)
                {
                    var imageId = Guid.NewGuid();

                    var pos = image.ImageName.LastIndexOf('.');
                    var fileextention = pos == -1 ? ".jpg" : image.ImageName.Substring(pos).ToLower();

                    var filenameLarge = "large-" + imageId + fileextention;
                    var filename = imageId.ToString() + fileextention;
                    var filenameMedium = "medium-" + imageId.ToString() + fileextention;

                    var uploadOrigin = CommonLib.UploadFile(image.ImageFile, filename, CommonLib.TypePath.Post);
                    var upload = CommonLib.UploadAndResizeImage(image.ImageFile, filenameLarge, 1000, CommonLib.TypePath.Post);
                    var upLoadMedium = CommonLib.UploadAndResizeImage(image.ImageFile, filenameMedium, 500, CommonLib.TypePath.Post);

                    if (!upload || !upLoadMedium || !uploadOrigin)
                    {
                        return new PostEditResponseModel
                        {
                            Code = "-1",
                            Message = "Upload image fail. Please check again"
                        }; // Upload image fail
                    }

                    // Insert new image
                    var imageData = new Image()
                    {
                        ImageId = imageId,
                        ImageTitle = image.ImageTitle,
                        AlbumId = albumId,
                        ImageLarge = filenameLarge,
                        ImageMedium = filenameMedium,
                        ImageOriginal = filename,
                        DeviceInfo = image.DeviceInfo,
                        LocationInfo = image.LocationInfo,
                        ImageCreateDate = DateTime.UtcNow,
                        ImageType = image.ImageType,
                        Status = 1,
                        ImageTags = image.ImageTags,
                        ImageStory = image.ImageStory
                    };

                    listImage.Add(imageData);
                }
                #endregion

                DataContext.Relations.Add(relation);
                DataContext.Albums.Add(album);
                listImage.ForEach(img=>DataContext.Images.Add(img));

                DataContext.SaveChanges();

                return new PostEditResponseModel
                    {
                        Code = "1",
                        Message = albumId.ToString()
                    };

            }
            catch (Exception exception)
            {
                return new PostEditResponseModel
                    {
                        Code = "-1",
                        Message = exception.Message
                    };
            }
        }