public void CreateMedia(Media media) { media.MediaID = Guid.NewGuid(); Db.Media.Add(media); Db.SaveChanges(); }
public SeedMedia(string versionID, ContentModel db, UmbracoProvider provider) : base(versionID, db, provider) { if (this.SeedCanRun) { Db = db; obsoleteMedia = new List<UObsoleteMedia>(); GetObsoleteUmbracoMedia(); SeedName = "Media"; if (SeedCanRun) { SeedStart(); MediaDirectory defaultDir = new MediaDirectory { Name = "Media", AuthorID= Guid.Parse("BD808F49-DEE0-4EC4-9024-DEC2A716948A"), Created = DateTime.Now, Modified =null, DirectoryID = Guid.Parse("710FE0A0-8894-40DB-8D7D-2FCBD7BA14CF"), ParentID = null }; if (Db.MediaDirectories.Find(defaultDir.DirectoryID.Value) == null) { Db.MediaDirectories.Add(defaultDir); } MediaDirectory obsoleteDir = new MediaDirectory { Name = "Obsolete", AuthorID = Guid.Parse("BD808F49-DEE0-4EC4-9024-DEC2A716948A"), Created= DateTime.Now, Modified=null, ParentID = defaultDir.DirectoryID.Value, DirectoryID = Guid.Parse("19D0268E-4ACA-41F5-BC14-77DC4C1343CD") }; if (Db.MediaDirectories.Find(obsoleteDir.DirectoryID.Value) == null) { Db.MediaDirectories.Add(obsoleteDir); } foreach (UObsoleteMedia media in obsoleteMedia) { if (media == null) { throw new Exception("MEDIA IS NULL"); } if (Db == null) { throw new Exception("DB IS NULL"); } if (obsoleteDir == null || defaultDir == null) { throw new Exception("ONE OF THE DIRECTORY OBJ ARE NULL"); } if (Db.Media == null) { throw new Exception("DB MEDIA NULL ERRROR"); } if (media.DataNVarchar != null) { if (media.ContentNodeID != null) { Shared.Entities.Media m = new Shared.Entities.Media(); m.Name = media.DataNVarchar.Split('/').Last(); m.MediaID = Guid.NewGuid(); m.DirectoryID = obsoleteDir.DirectoryID; m.FileName = media.DataNVarchar; m.ObsoleteID = media.ContentNodeID; m.FileMimeType = "none"; m.Created = DateTime.Now; m.Modified = null; m.AuthorID = Guid.Parse("BD808F49-DEE0-4EC4-9024-DEC2A716948A"); Db.Media.Add(m); m = new FC.Shared.Entities.Media(); } } } try { Db.SaveChanges(); SeedFinished(true); } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { HandleDbEntityValidationException(ex); throw ex; } } else { SeedFinished(true); } } }
public bool Handle() { MediaDirectory result = new MediaDirectory(); HttpFileCollection files = System.Web.HttpContext.Current.Request.Files; int filesLength = files.AllKeys.Count()-1; result.DirectoryID = Guid.Parse(HttpContext.Current.Request.Form["DirectoryID"]); HttpContext.Current.Response.Headers.Add("content-type", "application/json"); string abspath = HttpContext.Current.Server.MapPath("~/" + MediaRootDir); for (int i = 0;i <= filesLength; i++) { if(Directory.Exists(abspath)) { try { HttpPostedFile f = files[i]; Media m = new Media { MediaID = Guid.NewGuid(), DirectoryID = result.DirectoryID, Name = f.FileName }; string path = abspath + "/" + result.DirectoryID.ToString(); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } if (File.Exists(path + "/" + f.FileName)) { int count = Directory.GetFileSystemEntries(path).Where(w => w.Contains(Path.GetFileNameWithoutExtension(path + "/" + f.FileName))).Count(); FileInfo fi = new FileInfo(path + "/" + f.FileName); string nFileName = Path.GetFileNameWithoutExtension(path + "/" + f.FileName) + "_" + count +fi.Extension; m.Name = Path.GetFileNameWithoutExtension(path + "/" + f.FileName) + "_" + count + fi.Extension; m.FileName = MediaRootDir + result.DirectoryID + '/' + m.Name; f.SaveAs(path + "/" + nFileName); } else { f.SaveAs(path + "/" + f.FileName); m.Name = f.FileName; m.FileName = MediaRootDir + result.DirectoryID + '/' + f.FileName; } if (File.Exists(HttpContext.Current.Server.MapPath("~/" + m.FileName))) { repo.CreateMedia(m); } else { throw new Exception("File: "+ HttpContext.Current.Server.MapPath("~/" + m.FileName)+" not exists" ); } } catch(Exception ex) { throw ex; } } else { throw new DirectoryNotFoundException("The path: "+abspath+" is not found!"); } } return true; }
public void UpdateMedia(Media media) { Db.Entry<Media>(media).State = System.Data.Entity.EntityState.Modified; Db.SaveChanges(); }