private void DoImports(int id, Import importModule, Opus opus, IEnumerable <ResourceFile> docxRes, bool importOverwrite) { try { // import all files in one single step foreach (byte[] docx in docxRes.Select(item => BlobFactory.GetBlobStorage(item.ResourceId, BlobFactory.Container.Resources)).Select(b => b.Content)) { // import importModule.CreateHtmlFragments(docx); } // once done store fragments if (importModule.Fragments.Any()) { // get the Opus we add the content to var opusElement = Ctx.Elements.OfType <Opus>().FirstOrDefault(e => e.Project.Id == id && e.Id == opus.Id); if (importOverwrite && opusElement.HasChildren()) { opusElement.Children.ToList().ForEach(e => Ctx.Elements.Remove(e)); Ctx.SaveChanges(); } ReparentFragments(importModule.Fragments, opusElement); } } catch (Exception ex) { // handle error Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(ex.Message); Console.ForegroundColor = ConsoleColor.White; } }
/// <summary> /// Will create Duplicate record /// </summary> /// <param name="id">selected record id</param> /// <param name="projectId">Current Project Id</param> /// <param name="type">current tab</param> /// <param name="label">name </param> /// <param name="blobContent">blob</param> /// <param name="userName">name of the logged user</param> /// <param name="newName">new file name</param> /// <param name="resID">resource ID</param> /// <param name="mimetype">extension of the file</param> public int DuplicateResources(int id, int projectId, TypeOfResource type, string label, byte[] blobContent, string userName, string newName, Guid resID, string mimetype) { var project = ProjectManager.Instance.GetProject(projectId, userName); var volume = GetOrAddVolumeFolder(project, type); var parent = String.IsNullOrEmpty(label) ? volume : GetFolders(volume).SingleOrDefault(f => f.Name == label); if (parent == null) { // no folder means new label parent = CreateFolder(volume, label); } try { var resId = Guid.NewGuid(); using (var newBlob = BlobFactory.GetBlobStorage(resId, BlobFactory.Container.Resources)) { newBlob.Content = new byte[blobContent.Length]; blobContent.CopyTo(newBlob.Content, 0); var res = new ResourceFile { Name = newName, Parent = parent, Private = false, OrderNr = parent.Children.Max(r => r.OrderNr) + 1, TypesOfResource = type, ResourceId = resId, Project = project, Owner = Ctx.Users.Single(u => u.UserName == userName), MimeType = mimetype }; newBlob.Save(); return(AddResource(res)); } } catch (Exception) { } return(0); }
public byte[] GetFileData(int id, BlobFactory.Container blobContainer) { var res = GetFile(id); var blob = BlobFactory.GetBlobStorage(res.ResourceId, blobContainer); return(blob.Content); }
public Import LoadImport(string name, int projectId) { var res = Ctx.Resources.OfType <ResourceFile>().SingleOrDefault(r => r.Name == name && r.Project.Id == projectId && r.MimeType == "application/x-import"); Import import; if (res == null) { return(null); } using (var blob = BlobFactory.GetBlobStorage(res.ResourceId, BlobFactory.Container.Resources)) { if (blob.Content == null) { // something went wrong here, res exists but blob not, we'll trying some auto healing here import = new Import() { ProjectId = projectId }; SaveImport(import, res.Id, name); } else { import = Import.Deserialize(blob.Content); } } return(import); }
public static DTOBase Create(ResourceFile info, Root root) { if (info == null) { throw new ArgumentNullException("info"); } if (root == null) { throw new ArgumentNullException("root"); } var parent = info.Parent; using (var blob = BlobFactory.GetBlobStorage(info.ResourceId, BlobFactory.Container.Resources)) { var response = new FileDTO { Read = 1, Write = root.IsReadOnly ? (byte)0 : (byte)1, Locked = root.IsLocked ? (byte)1 : (byte)0, Name = info.Name, Size = blob.Content == null ? 0 : blob.Content.Length, UnixTimeStamp = (long)(info.ModifiedAt - _unixOrigin).TotalSeconds, Mime = Helper.GetMimeType(info.Name) ?? info.MimeType, Hash = root.VolumeId + Helper.EncodePath(info.ResourceId), ParentHash = root.VolumeId + Helper.EncodePath(parent != null ? parent.ResourceId : root.Directory.ResourceId), ThumbnailId = info.Id }; return(response); } }
public void AddMetaDataToResource(int id, string key, object value) { var res = GetFile(id); var blob = BlobFactory.GetBlobStorage(res.ResourceId, BlobFactory.Container.Resources); blob.AddOrUpdateMetaData(key, value); blob.Save(); }
public void SaveResource(int id, string filename, byte[] content, string userName) { var res = GetFile(id, userName); var blob = BlobFactory.GetBlobStorage(res.ResourceId, BlobFactory.Container.Resources); blob.Content = content; blob.Save(); res.Name = filename; SaveChanges(); }
JsonResult IDriver.Get(string target) { var fullPath = ParsePath(target); var answer = new GetResponse(); using (var blob = BlobFactory.GetBlobStorage(fullPath.File.ResourceId, BlobFactory.Container.Resources)) { answer.Content = Encoding.ASCII.GetString(blob.Content); } return(Json(answer)); }
internal Size GetImageDimension(ResourceFile file) { using (var blob = BlobFactory.GetBlobStorage(file.ResourceId, BlobFactory.Container.Resources)) { using (var ms = new MemoryStream()) { ms.Write(blob.Content, 0, blob.Content.Length); using (var image = Image.FromStream(ms)) { return(new Size(image.Width, image.Height)); } } } }
public ResourceFile GetFile(int id, BlobFactory.Container blobContainer = BlobFactory.Container.Resources) { var res = Ctx.Resources.OfType <ResourceFile>().FirstOrDefault(r => r.Id == id); if (res != null) { var blob = BlobFactory.GetBlobStorage(res.ResourceId, blobContainer); res.Metadata = blob.MetaData; } return(res); }
JsonResult IDriver.Put(string target, string content) { var fullPath = ParsePath(target); var answer = new ChangedResponse(); using (var blob = BlobFactory.GetBlobStorage(fullPath.File.ResourceId, BlobFactory.Container.Resources)) { blob.Content = Encoding.ASCII.GetBytes(content); blob.Save(); } answer.Changed.Add((FileDTO)DTOBase.Create(fullPath.File, fullPath.Root)); return(Json(answer)); }
public void EmptyFolder(Guid resId) { var childrenToDelete = GetFolder(resId).Children; childrenToDelete.ForEach(c => Delete(c.ResourceId)); foreach (var resource in childrenToDelete) { using (var blob = BlobFactory.GetBlobStorage(resource.ResourceId, BlobFactory.Container.Resources)) { blob.Remove(); } } SaveChanges(); }
public bool EmptyVolume(int projectId, TypeOfResource typeOfResource, string userName) { var files = Ctx.Resources.Where(r => r.Project.Id == projectId && r.TypesOfResource == typeOfResource && r.Owner.UserName == userName); foreach (var file in files) { Ctx.Resources.Remove(file); using (var blob = BlobFactory.GetBlobStorage(file.ResourceId, BlobFactory.Container.Resources)) { blob.Remove(); } } return(SaveChanges() > 0); }
/// <summary> /// deletes the record specifed based on id and resource id /// </summary> /// <param name="guid">Resource ID</param> /// <param name="resId">record ID</param> /// <param name="removeEmptyFolder"></param> /// <param name="resourcesDir"></param> /// <returns>Deletes the selected record</returns> public bool Delete(Guid guid, int resId, bool removeEmptyFolder, BlobFactory.Container resourcesDir = BlobFactory.Container.Resources) { try { var res = Ctx.Resources.FirstOrDefault(e => e.ResourceId == guid && e.Id == resId); if (res == null) { return(false); } // do not remove any deployed files if (Ctx.Published.Any(p => p.ResourceFiles.Any(r => r.ResourceId == guid && r.Id == resId))) { return(false); } if (res.HasChildren()) { // TODO: delete children in recursive order } else { // keep ref to current parent var parent = res.Parent; var blob = BlobFactory.GetBlobStorage(res.ResourceId, resourcesDir); // if already in trash, remove if (res.TypesOfResource == TypeOfResource.Trash) { Ctx.Resources.Remove(res); blob.Remove(); } else { // Move to trash, de-parent because trash is flat var p = Ctx.Projects.Find(res.Project.Id); res.TypesOfResource = TypeOfResource.Trash; res.Deleted = true; res.Parent = Ctx.Resources.OfType <ResourceFolder>().FirstOrDefault(r => r.Parent == null && r.TypesOfResource == TypeOfResource.Trash && r.Project.Id == p.Id); res.Project = p; } SaveChanges(); // remove folder if there is no file in it (this is due to the relatively flat structure in file explorer) if (removeEmptyFolder && !parent.Children.Any()) { Ctx.Resources.Remove(parent); SaveChanges(); } } return(true); } catch (Exception ex) { Debug.WriteLine(ex.Message); return(false); } }
public void SaveImport(Import import, int resoureceId, string newName = null) { var projectId = import.ProjectId; var prj = Ctx.Projects .Include(p => p.Team) .Include(p => p.Team.Members) .Include(p => p.Team.Members.Select(m => m.Role)) .Single(p => p.Id == projectId); if (!String.IsNullOrEmpty(newName)) { import.ImportName = newName; } var owner = prj.Team.Members.First(m => m.TeamLead); var res = Ctx.Resources.OfType <ResourceFile>().SingleOrDefault(r => r.Name == import.ImportName && r.Project.Id == projectId && r.MimeType == "application/x-import"); AssureResFolder(prj, owner); if (res == null) { // first time save to DB res = new ResourceFile { ResourceId = Guid.NewGuid(), MimeType = "application/x-import", Owner = owner.Member, Project = prj, Name = import.ImportName, Parent = null, Private = true, Deleted = false, TypesOfResource = TypeOfResource.Import, OrderNr = 0 }; Ctx.Resources.Add(res); Ctx.SaveChanges(); } // always refresh blob store using (var blob = BlobFactory.GetBlobStorage(res.ResourceId, BlobFactory.Container.Resources)) { blob.Content = import.Serialize(); blob.Save(); } // after storing as common we assign it to the particular import file var importFile = Ctx.Resources.FirstOrDefault(r => r.Id == resoureceId && r.Project.Id == projectId); if (importFile != null) { using (var blob = BlobFactory.GetBlobStorage(importFile.ResourceId, BlobFactory.Container.Resources)) { blob.AddOrUpdateMetaData("Mapping", import); blob.Save(); } } }
public int AddResource(int projectId, TypeOfResource type, string label, string fileName, string fileContentType, byte[] content, string userName) { var project = ProjectManager.Instance.GetProject(projectId, userName); var user = Ctx.Users.Single(u => u.UserName == userName); // get label var parent = Ctx.Resources.OfType <ResourceFolder>().SingleOrDefault(f => f.Name == label && f.TypesOfResource == type && f.Parent == null && f.Project.Id == projectId); // add if not present if (parent == null) { // no folder means new label, if label provided parent = new ResourceFolder { TypesOfResource = type, Name = label ?? String.Empty, Owner = user, OrderNr = 1, Private = false, Project = project }; // add before usage Ctx.Resources.Add(parent); SaveChanges(); } var resId = Guid.NewGuid(); ResourceFile res = null; using (var blob = BlobFactory.GetBlobStorage(resId, BlobFactory.Container.Resources)) { try { blob.Content = content; res = new ResourceFile { Name = fileName, Parent = parent, Private = false, OrderNr = parent.Children.Any() ? parent.Children.Max(r => r.OrderNr) + 1 : 1, TypesOfResource = type, ResourceId = resId, Project = project, Owner = user, MimeType = fileContentType }; blob.Save(); return(AddResource(res)); } catch (Exception) { } } return(0); }
public bool Delete(Guid guid, bool removeEmptyFolder = false, BlobFactory.Container resourcesDir = BlobFactory.Container.Resources) { try { var res = Ctx.Resources.FirstOrDefault(e => e.ResourceId == guid); if (res == null) { return(false); } // do not remove any deployed files if (Ctx.Published.Any(p => p.ResourceFiles.Any(r => r.ResourceId == guid))) { return(false); } if (res.HasChildren()) { // delete children in recursive order } else { var blob = BlobFactory.GetBlobStorage(res.ResourceId, resourcesDir); // if already in trash, remove if (res.TypesOfResource == TypeOfResource.Trash) { Ctx.Resources.Remove(res); blob.Remove(); } else { // Move to trash, de-parent because trash is flat var p = Ctx.Projects.Find(res.Project.Id); res.TypesOfResource = TypeOfResource.Trash; res.Deleted = true; res.Parent = Ctx.Resources.OfType <ResourceFolder>().FirstOrDefault(r => r.Parent == null && r.TypesOfResource == TypeOfResource.Trash && r.Project.Id == p.Id); res.Project = p; } } SaveChanges(); return(true); } catch (Exception ex) { Debug.WriteLine(ex.Message); return(false); } }
public override void ExecuteResult(ControllerContext context) { var response = context.HttpContext.Response; string fileName; var fileNameEncoded = HttpUtility.UrlEncode(File.Name); if (context.HttpContext.Request.UserAgent.Contains("MSIE")) { // IE < 9 do not support RFC 6266 (RFC 2231/RFC 5987) fileName = "filename=\"" + fileNameEncoded + "\""; } else { fileName = "filename*=UTF-8\'\'" + fileNameEncoded; // RFC 6266 (RFC 2231/RFC 5987) } string mime; string disposition; if (IsDownload) { mime = "application/octet-stream"; disposition = "attachment; " + fileName; } else { mime = Helper.GetMimeType(File.Name) ?? File.MimeType; disposition = (mime.Contains("image") || mime.Contains("text") || mime == "application/x-shockwave-flash" ? "inline; " : "attachment; ") + fileName; } using (var blob = BlobFactory.GetBlobStorage(File.ResourceId, BlobFactory.Container.Resources)) { response.ContentType = mime; response.AppendHeader("Content-Disposition", disposition); response.AppendHeader("Content-Location", File.Name); response.AppendHeader("Content-Transfer-Encoding", "binary"); response.AppendHeader("Content-Length", blob.Content != null ? blob.Content.Length.ToString() : "0"); if (blob.Content != null) { response.BinaryWrite(blob.Content); } } response.End(); response.Flush(); }
public void DeleteUserFilePermanently(int userFileId, string userName) { var uf = Ctx.UserFiles .FirstOrDefault(u => u.Owner.UserName == userName && u.Id == userFileId); if (uf == null) { return; } var id = uf.ResourceId; Ctx.UserFiles.Remove(uf); if (SaveChanges() != 1) { return; } using (var blob = BlobFactory.GetBlobStorage(id, BlobFactory.Container.UserFolder)) { blob.Remove(); } }
JsonResult IDriver.MakeFile(string target, string name) { var fullPath = ParsePath(target); var newFile = new ResourceFile { Name = name, Parent = fullPath.Directory, MimeType = Helper.GetMimeType(name), Owner = fullPath.Directory.Owner, TypesOfResource = fullPath.Directory.TypesOfResource, ResourceId = Guid.NewGuid(), Private = fullPath.Directory.Private, Project = fullPath.Directory.Project }; using (var blob = BlobFactory.GetBlobStorage(newFile.ResourceId, BlobFactory.Container.Resources)) { blob.Save(); } ResourceManager.Instance.AddResource(newFile); return(Json(new AddResponse(newFile, fullPath.Root))); }
public ResourceFile CopyFile(ResourceFile resourceFile, ResourceFolder resourceFolder) { var newResId = Guid.NewGuid(); try { using (var blob = BlobFactory.GetBlobStorage(resourceFile.ResourceId, BlobFactory.Container.Resources)) { using (var newBlob = BlobFactory.GetBlobStorage(newResId, BlobFactory.Container.Resources)) { newBlob.Content = blob.Content; newBlob.Save(); var newFile = new ResourceFile(); resourceFile.CopyProperties <ResourceFile>(newFile); newFile.Parent = resourceFolder; newFile.ResourceId = newResId; SaveChanges(); return(newFile); } } } catch (Exception) { // something went wrong, remove file ? return(resourceFile); } }
/// <summary> /// This function stores a media file in blob storage and returns a serializable reference object /// for the order process. /// </summary> /// <param name="fileName">The internal name</param> /// <param name="group">The media type, such as 'epub', 'pdf'</param> /// <param name="content">The actual content</param> /// <param name="userName">The user this content is applied to.</param> /// <returns></returns> public MediaFile StoreMediaFile(string fileName, GroupKind group, byte[] content, string userName) { if (content == null) { return(null); } var id = Guid.NewGuid(); using (var blob = BlobFactory.GetBlobStorage(id, BlobFactory.Container.MediaFiles)) { blob.Content = content; var fileRes = new UserFile { Name = fileName, Owner = userName == null ? null : GetCurrentUser(userName), ResourceId = id, Folder = BlobFactory.Container.MediaFiles.ToString(), Private = true }; Ctx.UserFiles.Add(fileRes); SaveChanges(); blob.Save(); } return(new MediaFile(id, group.ToString().ToLowerInvariant())); }
JsonResult IDriver.Upload(string target, System.Web.HttpFileCollectionBase targets) { var dest = ParsePath(target); var response = new AddResponse(); if (dest.Root.MaxUploadSize.HasValue) { for (int i = 0; i < targets.AllKeys.Length; i++) { HttpPostedFileBase file = targets[i]; if (file.ContentLength > dest.Root.MaxUploadSize.Value) { return(Error.MaxUploadFileSize()); } } } for (int i = 0; i < targets.AllKeys.Length; i++) { var file = targets[i]; var newFile = new ResourceFile { Name = file.FileName, Parent = dest.Directory, Owner = dest.Directory.Owner, ResourceId = Guid.NewGuid(), Private = dest.Directory.Private, TypesOfResource = dest.Directory.TypesOfResource, Project = dest.Directory.Project, MimeType = Helper.GetMimeType(file.FileName) }; using (var blob = BlobFactory.GetBlobStorage(newFile.ResourceId, BlobFactory.Container.Resources)) { if (dest.Root.UploadOverwrite) { //if file already exist we rename the current file, //and if upload is succesfully delete temp file, in otherwise we restore old file var uploaded = false; try { var bytes = new byte[file.InputStream.Length]; file.InputStream.Seek(0, SeekOrigin.Begin); file.InputStream.Read(bytes, 0, bytes.Length); blob.Content = bytes; blob.Save(); uploaded = true; } catch { } finally { if (!uploaded) { blob.Remove(); } } } else { throw new NotImplementedException(""); } ResourceManager.Instance.AddResource(newFile); response.Added.Add((FileDTO)DTOBase.Create(newFile, dest.Root)); } } if (!HttpContext.Current.Request.AcceptTypes.Contains("application/json")) { return(Json(response, "text/html")); } else { return(Json(response)); } }
private Guid?StoreBlobImage(int projectId, string fileName, Stream data) { var importStore = Ctx.Resources .Include("Owner") .Include("Project") .OfType <ResourceFolder>() .Single(r => r.Project.Id == projectId && r.TypesOfResource == TypeOfResource.Import && r.Parent == null); // Assure designated image folder in store ResourceFolder imgFolder = null; if (importStore.HasChildren()) { imgFolder = importStore.Children.OfType <ResourceFolder>().FirstOrDefault(r => r.Name == "Images"); } else { importStore.Children = new List <Resource>(); } if (imgFolder == null) { imgFolder = new ResourceFolder { Name = "Converted Images", Owner = importStore.Owner, Project = importStore.Project, Private = false, Deleted = false, TypesOfResource = TypeOfResource.Import, ResourceId = Guid.NewGuid(), Parent = importStore, Children = new List <Resource>() }; Ctx.Resources.Add(imgFolder); } // Read Image to convert to JPEG Image img = Image.FromStream(data); var o = 0; var resFile = new ResourceFile { Name = System.IO.Path.GetFileNameWithoutExtension(fileName) + ".jpg", MimeType = "image/jpeg", Owner = importStore.Owner, Project = importStore.Project, Private = false, Deleted = false, OrderNr = o++, TypesOfResource = TypeOfResource.Import, ResourceId = Guid.NewGuid(), Parent = imgFolder }; Ctx.Resources.Add(resFile); Ctx.SaveChanges(); // save resources physically to blob storage using (var blob = BlobFactory.GetBlobStorage(resFile.ResourceId, BlobFactory.Container.Resources)) { using (var ms = new MemoryStream()) { img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); blob.Content = ms.ToArray(); } blob.Save(); } return(resFile.ResourceId); }