//获取某视频的标签 public TAG[] GetTagsOf(string video_id) { VIDEOS v = db.VIDEOS.Find(video_id); TAG[] tags = new TAG[v.TAG.Count]; for (int i = 0; i < v.TAG.Count; i++) { tags[i] = v.TAG.ElementAt(i); } return(tags); }
/*Index-->*/ //获取某视频的标签 public TAG[] GetTagsOfVideo(string video_id) { VIDEOS v = db.VIDEOS.Find(video_id); int num = 0; foreach (var item in v.TAG) { num++; } TAG[] tags = new TAG[num]; for (int i = 0; i < num; i++) { tags[i] = v.TAG.ElementAt(i); } return(tags); }
//上传视频 public ActionResult UploadVideoDB(FormCollection formdata) { HttpPostedFileBase file = Request.Files["video_upload_name"]; HttpPostedFileBase cover = Request.Files["cover_upload_name"]; var zoneName = Request.Form["zone_name"]; var privacy = Request.Form["privacy"]; var title = Request.Form["title_name"]; var tagNames = Request.Form["tag_name"]; var description = Request.Form["description_name"]; var uploaderId = Request.Form["user_id"]; if (file == null || cover == null) { Dictionary <string, string> JData = new Dictionary <string, string>(); JData.Add("Flag", "false"); return(Json(JsonConvert.SerializeObject(JData, Formatting.Indented))); } //判断各个参数是否存在 var z = db.ZONE.Find(zoneName); if (z == null || privacy == null || title == null || tagNames == null || tagNames == "") { Dictionary <string, string> JData = new Dictionary <string, string>(); JData.Add("Flag", "false"); return(Json(JsonConvert.SerializeObject(JData, Formatting.Indented))); } //video实例 VIDEOS video = new VIDEOS(); video.VIDEO_ID = "0"; if (privacy == "all") { video.PRIVACY = true; } else { video.PRIVACY = false; } video.TITLE = title; video.DESCRIPTION = description; video.LIKES_NUM = 0; video.PLAY_NUM = 0; video.COLLECT_NUM = 0; video.SHARE_NUM = 0; video.UPLOAD_TIME = DateTime.Now; USERS uploader = db.USERS.Find(uploaderId); //加入VIDEOS元组 if (ModelState.IsValid) { db.VIDEOS.Add(video); db.SaveChanges(); } else { Dictionary <string, string> JsonD = new Dictionary <string, string>(); JsonD.Add("Flag", "false"); return(Json(JsonConvert.SerializeObject(JsonD, Formatting.Indented))); } //获取刚新建的元组 var Ids = db.Database.SqlQuery <string>("select video_id from videos where upload_time=(select max(upload_time) from videos)").ToList(); string newId = Ids[0]; video = db.VIDEOS.Find(newId); //联系集Upload_Video var uv = new UPLOAD_VIDEO(); uv.USER_ID = uploader.USER_ID; uv.VIDEO_ID = video.VIDEO_ID; db.UPLOAD_VIDEO.Add(uv); db.SaveChanges(); //联系集Has_Tag string[] names = tagNames.Split(';'); for (int i = 0; i < names.Length; i++) { var tag = db.TAG.Find(names[i]); if (tag == null) { tag = new TAG(); tag.TAG_NAME = names[i]; tag.REFER_NUM = 0; db.TAG.Add(tag); db.SaveChanges(); } tag.REFER_NUM++; video.TAG.Add(tag); db.SaveChanges(); } //联系集Has_Zone var hz = new HAS_ZONE(); hz.VIDEO_ID = video.VIDEO_ID; hz.ZONE_NAME = z.ZONE_NAME; db.HAS_ZONE.Add(hz); db.SaveChanges(); z.VIDEO_NUM++; db.Entry(z).State = EntityState.Modified; db.SaveChanges(); //存视频文件 var filePath = "C:\\Users\\16214\\Desktop\\iVlog\\iVlog\\source\\file\\v\\" + newId; if (!System.IO.Directory.Exists(filePath)) { System.IO.Directory.CreateDirectory(filePath); } var fileName = file.FileName; file.SaveAs(Path.Combine(filePath, fileName)); filePath = "../../source/file/v/" + newId; video.PATH = Path.Combine(filePath, fileName); db.Entry(video).State = EntityState.Modified; db.SaveChanges(); //存封面文件 var coverPath = "C:\\Users\\16214\\Desktop\\iVlog\\iVlog\\source\\file\\c\\" + newId; if (!System.IO.Directory.Exists(coverPath)) { System.IO.Directory.CreateDirectory(coverPath); } var coverName = cover.FileName; file.SaveAs(Path.Combine(coverPath, coverName)); coverPath = "../../source/file/c/" + newId; video.COVER = Path.Combine(coverPath, coverName); db.Entry(video).State = EntityState.Modified; db.SaveChanges(); //发布消息 var ur = from a in db.USER_RELATIONSHIP where a.PASSIVE_USER_ID == uploaderId select a.ACTIVE_USER_ID; foreach (var item in ur) { SendMessage(4, uploaderId, item); } Dictionary <string, string> JsonData = new Dictionary <string, string>(); JsonData.Add("Flag", "true"); return(Json(JsonConvert.SerializeObject(JsonData, Formatting.Indented))); }