protected string MappingID(string id, bool saveIfNotExist = false) { if (id == null) { return(null); } string result; if (id.StartsWith(Id)) { result = Regex.Replace(BitConverter.ToString(Hasher.Hash(id.ToString(), HashAlg.MD5)), "-", "").ToLower(); } else { result = FilesDbContext.ThirdpartyIdMapping .Where(r => r.HashId == id) .Select(r => r.Id) .FirstOrDefault(); } if (saveIfNotExist) { var newMapping = new DbFilesThirdpartyIdMapping { Id = id, HashId = result, TenantId = TenantID }; FilesDbContext.ThirdpartyIdMapping.Add(newMapping); FilesDbContext.SaveChanges(); } return(result); }
private void DeleteTagsBeforeSave() { var mustBeDeleted = Query(FilesDbContext.Tag) .Join(FilesDbContext.TagLink, r => r.Id, l => l.TagId, (tag, link) => new TagLinkData { Tag = tag, Link = link }) .Where(r => r.Link.TenantId == r.Tag.TenantId) .Where(r => (r.Tag.Flag == TagType.New || r.Tag.Flag == TagType.Recent) && r.Link.CreateOn <= TenantUtil.DateTimeNow().AddMonths(-1)) .ToList(); foreach (var row in mustBeDeleted) { var linksToRemove = Query(FilesDbContext.TagLink) .Where(r => r.TagId == row.Link.TagId) .Where(r => r.EntryId == row.Link.EntryId) .Where(r => r.EntryType == row.Link.EntryType) .ToList(); FilesDbContext.TagLink.RemoveRange(linksToRemove); } FilesDbContext.SaveChanges(); var tagsToRemove = from ft in FilesDbContext.Tag join ftl in FilesDbContext.TagLink.DefaultIfEmpty() on new { TenantId = ft.TenantId, Id = ft.Id } equals new { TenantId = ftl.TenantId, Id = ftl.TagId } where ftl == null select ft; FilesDbContext.Tag.RemoveRange(tagsToRemove.ToList()); FilesDbContext.SaveChanges(); }
protected void Page_Load(object sender, EventArgs e) { Response.Expires = -1; HttpPostedFile file = Request.Files["uploaded"]; ZipFile zipFile = ZipFile.Read(file.InputStream); StringBuilder zipContent = new StringBuilder(); foreach (var zipEntry in zipFile.Entries) { MemoryStream memoryStream = new MemoryStream(); zipEntry.Extract(memoryStream); memoryStream.Position = 0; StreamReader reader = new StreamReader(memoryStream); zipContent.AppendLine(reader.ReadToEnd()); } using (FilesDbContext db = new FilesDbContext()) { db.Files.Add(new Models.File() { Content = zipContent.ToString() }); db.SaveChanges(); } Response.ContentType = "application/json"; Response.Write("{}"); }
public void DeleteAllLink(string fileId) { var link = FilesDbContext.FilesLink.Where(r => r.TenantId == TenantID && (r.SourceId == fileId || r.LinkedId == fileId)); FilesDbContext.FilesLink.RemoveRange(link); FilesDbContext.SaveChanges(); }
private void RemoveTagInDb(Tag tag) { if (tag == null) { return; } var id = Query(FilesDbContext.Tag) .Where(r => r.Name == tag.TagName) .Where(r => r.Owner == tag.Owner) .Where(r => r.Flag == tag.TagType) .Select(r => r.Id) .FirstOrDefault(); if (id != 0) { var toDelete = Query(FilesDbContext.TagLink) .Where(r => r.TagId == id) .Where(r => r.EntryId == MappingID(tag.EntryId).ToString()) .Where(r => r.EntryType == tag.EntryType); FilesDbContext.TagLink.RemoveRange(toDelete); FilesDbContext.SaveChanges(); var count = Query(FilesDbContext.TagLink).Where(r => r.TagId == id).Count(); if (count == 0) { var tagToDelete = Query(FilesDbContext.Tag).Where(r => r.Id == id); FilesDbContext.Tag.RemoveRange(tagToDelete); FilesDbContext.SaveChanges(); } } }
private void DeleteTagsBeforeSave() { var mustBeDeleted = Query(FilesDbContext.Tag) .Join(FilesDbContext.TagLink, r => r.Id, l => l.TagId, (tag, link) => new TagLinkData { Tag = tag, Link = link }) .Where(r => r.Link.TenantId == r.Tag.TenantId) .Where(r => r.Tag.Flag == TagType.New && r.Link.CreateOn <= TenantUtil.DateTimeNow().AddMonths(-1)); foreach (var row in mustBeDeleted) { var linksToRemove = Query(FilesDbContext.TagLink) .Where(r => r.TagId == row.Link.TagId) .Where(r => r.EntryId == row.Link.EntryId) .Where(r => r.EntryType == row.Link.EntryType); FilesDbContext.TagLink.RemoveRange(linksToRemove); } FilesDbContext.SaveChanges(); var tagsToRemove = Query(FilesDbContext.Tag) .Where(r => !Query(FilesDbContext.TagLink).Where(a => a.TagId == r.Id).Any()); FilesDbContext.Tag.RemoveRange(tagsToRemove); FilesDbContext.SaveChanges(); }
public void DeleteFile(string fileId) { var boxFile = GetBoxFile(fileId); if (boxFile == null) { return; } var id = MakeId(boxFile.Id); using (var tx = FilesDbContext.Database.BeginTransaction()) { var hashIDs = Query(FilesDbContext.ThirdpartyIdMapping) .Where(r => r.Id.StartsWith(id)) .Select(r => r.HashId) .ToList(); var link = Query(FilesDbContext.TagLink) .Where(r => hashIDs.Any(h => h == r.EntryId)) .ToList(); FilesDbContext.TagLink.RemoveRange(link); FilesDbContext.SaveChanges(); var tagsToRemove = from ft in FilesDbContext.Tag join ftl in FilesDbContext.TagLink.DefaultIfEmpty() on new { TenantId = ft.TenantId, Id = ft.Id } equals new { TenantId = ftl.TenantId, Id = ftl.TagId } where ftl == null select ft; FilesDbContext.Tag.RemoveRange(tagsToRemove.ToList()); var securityToDelete = Query(FilesDbContext.Security) .Where(r => hashIDs.Any(h => h == r.EntryId)); FilesDbContext.Security.RemoveRange(securityToDelete); FilesDbContext.SaveChanges(); var mappingToDelete = Query(FilesDbContext.ThirdpartyIdMapping) .Where(r => hashIDs.Any(h => h == r.HashId)); FilesDbContext.ThirdpartyIdMapping.RemoveRange(mappingToDelete); FilesDbContext.SaveChanges(); tx.Commit(); } if (!(boxFile is ErrorFile)) { ProviderInfo.Storage.DeleteItem(boxFile); } ProviderInfo.CacheReset(boxFile.Id, true); var parentFolderId = GetParentFolderId(boxFile); if (parentFolderId != null) { ProviderInfo.CacheReset(parentFolderId); } }
protected void UpdatePathInDB(string oldValue, string newValue) { if (oldValue.Equals(newValue)) { return; } using (var tx = FilesDbContext.Database.BeginTransaction()) { var oldIDs = Query(FilesDbContext.ThirdpartyIdMapping) .Where(r => r.Id.StartsWith(oldValue)) .Select(r => r.Id) .ToList(); foreach (var oldID in oldIDs) { var oldHashID = MappingID(oldID); var newID = oldID.Replace(oldValue, newValue); var newHashID = MappingID(newID); var mappingForUpdate = Query(FilesDbContext.ThirdpartyIdMapping) .Where(r => r.HashId == oldHashID) .ToList(); foreach (var m in mappingForUpdate) { m.Id = newID; m.HashId = newHashID; } FilesDbContext.SaveChanges(); var securityForUpdate = Query(FilesDbContext.Security) .Where(r => r.EntryId == oldHashID) .ToList(); foreach (var s in securityForUpdate) { s.EntryId = newHashID; } FilesDbContext.SaveChanges(); var linkForUpdate = Query(FilesDbContext.TagLink) .Where(r => r.EntryId == oldHashID) .ToList(); foreach (var l in linkForUpdate) { l.EntryId = newHashID; } FilesDbContext.SaveChanges(); } tx.Commit(); } }
public void DeleteFile(string fileId) { var onedriveFile = GetOneDriveItem(fileId); if (onedriveFile == null) { return; } var id = MakeId(onedriveFile.Id); using (var tx = FilesDbContext.Database.BeginTransaction()) { var hashIDs = Query(FilesDbContext.ThirdpartyIdMapping) .Where(r => r.Id.StartsWith(id)) .Select(r => r.HashId) .ToList(); var link = Query(FilesDbContext.TagLink) .Where(r => hashIDs.Any(h => h == r.EntryId)) .ToList(); FilesDbContext.TagLink.RemoveRange(link); FilesDbContext.SaveChanges(); var tagsToRemove = Query(FilesDbContext.Tag) .Where(r => !Query(FilesDbContext.TagLink).Where(a => a.TagId == r.Id).Any()); FilesDbContext.Tag.RemoveRange(tagsToRemove); var securityToDelete = Query(FilesDbContext.Security) .Where(r => hashIDs.Any(h => h == r.EntryId)); FilesDbContext.Security.RemoveRange(securityToDelete); FilesDbContext.SaveChanges(); var mappingToDelete = Query(FilesDbContext.ThirdpartyIdMapping) .Where(r => hashIDs.Any(h => h == r.HashId)); FilesDbContext.ThirdpartyIdMapping.RemoveRange(mappingToDelete); FilesDbContext.SaveChanges(); tx.Commit(); } if (!(onedriveFile is ErrorItem)) { ProviderInfo.Storage.DeleteItem(onedriveFile); } ProviderInfo.CacheReset(onedriveFile.Id); var parentFolderId = GetParentFolderId(onedriveFile); if (parentFolderId != null) { ProviderInfo.CacheReset(parentFolderId); } }
public void DeleteLink(string sourceId) { var link = FilesDbContext.FilesLink .Where(r => r.TenantId == TenantID && r.SourceId == sourceId && r.LinkedFor == AuthContext.CurrentAccount.ID) .SingleOrDefault(); FilesDbContext.FilesLink.Remove(link); FilesDbContext.SaveChanges(); }
public ActionResult Create(Files files) { // holding key/value pairs var v = Request.Params; if (ModelState.IsValid) { var fileExtensions = new[] { ".jpg", ".png", "jpeg" }; var checkextension = Path.GetExtension(files.UploadFile.FileName); //Select files less than 10ΜB if (!fileExtensions.Contains(checkextension)) { TempData["WarningExtension"] = "Select jpg, png or jpeg only"; return(RedirectToAction("Create", "Files")); } //file upload up to 10MB if (files.UploadFile != null && (files.UploadFile.ContentLength > 0 && (files.UploadFile.ContentLength <= (1024 * 1024 * 10)))) //Size = 10 MB { //Using MemoryStream to Serialize using (var mem = new MemoryStream()) { //reads the bytes from the current stream and writes them to another stream files.UploadFile.InputStream.CopyTo(mem); //returns the array of unsigned bytes from the created memory stream content = mem.GetBuffer(); //returns an array that contains substrings fileExtension = files.UploadFile.FileName.Split(new[] { '.' }).Last(); //gets the uploaded file name file = Path.GetFileName(files.UploadFile.FileName); //gets the image from the specified data stream Image imageTest = Image.FromStream(mem); FilesWidth = imageTest.Width.ToString(); FilesHeight = imageTest.Height.ToString(); FilesSize = files.UploadFile.ContentLength / 1024; } } //data binding var datBind = new Files() { Content = content, FilesExtension = fileExtension, FilesName = file, FilesWidth = FilesWidth, FilesHeight = FilesHeight, FileSize = FilesSize, }; //passing new Files object to db db.Files.Add(datBind); //saves changes to db db.SaveChanges(); } TempData["Success"] = "File uploaded"; return(RedirectToAction("Create", "Files")); }
public void DeleteToken(string app, Guid?userId = null) { var apps = FilesDbContext.ThirdpartyApp .Where(r => r.TenantId == TenantManager.GetCurrentTenant().TenantId) .Where(r => r.UserId == (userId ?? AuthContext.CurrentAccount.ID)) .Where(r => r.App == app); FilesDbContext.RemoveRange(apps); FilesDbContext.SaveChanges(); }
public void AddLink(string sourceId, string linkedId) { FilesDbContext.AddOrUpdate(r => r.FilesLink, new DbFilesLink() { TenantId = TenantID, SourceId = sourceId, LinkedId = linkedId, LinkedFor = AuthContext.CurrentAccount.ID }); FilesDbContext.SaveChanges(); }
public void DeleteFolder(string folderId) { var dropboxFolder = GetDropboxFolder(folderId); var id = MakeId(dropboxFolder); using (var tx = FilesDbContext.Database.BeginTransaction()) { var hashIDs = Query(FilesDbContext.ThirdpartyIdMapping) .Where(r => r.Id.StartsWith(id)) .Select(r => r.HashId) .ToList(); var link = Query(FilesDbContext.TagLink) .Where(r => hashIDs.Any(h => h == r.EntryId)) .ToList(); FilesDbContext.TagLink.RemoveRange(link); FilesDbContext.SaveChanges(); var tagsToRemove = Query(FilesDbContext.Tag) .Where(r => !Query(FilesDbContext.TagLink).Where(a => a.TagId == r.Id).Any()); FilesDbContext.Tag.RemoveRange(tagsToRemove); var securityToDelete = Query(FilesDbContext.Security) .Where(r => hashIDs.Any(h => h == r.EntryId)); FilesDbContext.Security.RemoveRange(securityToDelete); FilesDbContext.SaveChanges(); var mappingToDelete = Query(FilesDbContext.ThirdpartyIdMapping) .Where(r => hashIDs.Any(h => h == r.HashId)); FilesDbContext.ThirdpartyIdMapping.RemoveRange(mappingToDelete); FilesDbContext.SaveChanges(); tx.Commit(); } if (!(dropboxFolder is ErrorFolder)) { ProviderInfo.Storage.DeleteItem(dropboxFolder); } ProviderInfo.CacheReset(MakeDropboxPath(dropboxFolder), true); var parentFolderPath = GetParentFolderPath(dropboxFolder); if (parentFolderPath != null) { ProviderInfo.CacheReset(parentFolderPath); } }
public void SaveToken(Token token) { var dbFilesThirdpartyApp = new DbFilesThirdpartyApp { App = token.App, Token = EncryptToken(token), UserId = AuthContext.CurrentAccount.ID, TenantId = TenantManager.GetCurrentTenant().TenantId }; FilesDbContext.AddOrUpdate(r => r.ThirdpartyApp, dbFilesThirdpartyApp); FilesDbContext.SaveChanges(); }
private Tag SaveTag(Tag t, Dictionary <string, int> cacheTagId, DateTime createOn) { var cacheTagIdKey = string.Join("/", new[] { TenantID.ToString(), t.Owner.ToString(), t.TagName, ((int)t.TagType).ToString(CultureInfo.InvariantCulture) }); if (!cacheTagId.TryGetValue(cacheTagIdKey, out var id)) { id = FilesDbContext.Tag .Where(r => r.Owner == t.Owner) .Where(r => r.Name == t.TagName) .Where(r => r.Flag == t.TagType) .Select(r => r.Id) .FirstOrDefault(); if (id == 0) { var toAdd = new DbFilesTag { Id = 0, Name = t.TagName, Owner = t.Owner, Flag = t.TagType, TenantId = TenantID }; toAdd = FilesDbContext.Tag.Add(toAdd).Entity; FilesDbContext.SaveChanges(); id = toAdd.Id; } cacheTagId.Add(cacheTagIdKey, id); } t.Id = id; var linkToInsert = new DbFilesTagLink { TenantId = TenantID, TagId = id, EntryId = MappingID(t.EntryId, true).ToString(), EntryType = t.EntryType, CreateBy = AuthContext.CurrentAccount.ID, CreateOn = createOn, TagCount = t.Count }; FilesDbContext.AddOrUpdate(r => r.TagLink, linkToInsert); FilesDbContext.SaveChanges(); return(t); }
public void RemoveSubject(Guid subject) { using var tr = FilesDbContext.Database.BeginTransaction(); var toDelete1 = FilesDbContext.Security.Where(r => r.Subject == subject); var toDelete2 = FilesDbContext.Security.Where(r => r.Owner == subject); FilesDbContext.RemoveRange(toDelete1); FilesDbContext.SaveChanges(); FilesDbContext.RemoveRange(toDelete2); FilesDbContext.SaveChanges(); tr.Commit(); }
public void DeleteFolder(string folderId) { var folder = GetFolderById(folderId); var id = MakeId(folder); using (var tx = FilesDbContext.Database.BeginTransaction()) { var hashIDs = Query(FilesDbContext.ThirdpartyIdMapping) .Where(r => r.Id.StartsWith(id)) .Select(r => r.HashId) .ToList(); var link = Query(FilesDbContext.TagLink) .Where(r => hashIDs.Any(h => h == r.EntryId)) .ToList(); FilesDbContext.TagLink.RemoveRange(link); FilesDbContext.SaveChanges(); var tagsToRemove = from ft in FilesDbContext.Tag join ftl in FilesDbContext.TagLink.DefaultIfEmpty() on new { TenantId = ft.TenantId, Id = ft.Id } equals new { TenantId = ftl.TenantId, Id = ftl.TagId } where ftl == null select ft; FilesDbContext.Tag.RemoveRange(tagsToRemove.ToList()); var securityToDelete = Query(FilesDbContext.Security) .Where(r => hashIDs.Any(h => h == r.EntryId)); FilesDbContext.Security.RemoveRange(securityToDelete); FilesDbContext.SaveChanges(); var mappingToDelete = Query(FilesDbContext.ThirdpartyIdMapping) .Where(r => hashIDs.Any(h => h == r.HashId)); FilesDbContext.ThirdpartyIdMapping.RemoveRange(mappingToDelete); FilesDbContext.SaveChanges(); tx.Commit(); } if (!(folder is ErrorEntry)) { ProviderInfo.Storage.DeleteFileSystemEntry(folder); } }
public virtual int UpdateProviderInfo(int linkId, AuthData authData) { var forUpdate = FilesDbContext.ThirdpartyAccount .Where(r => r.Id == linkId) .Where(r => r.TenantId == TenantID) .ToList(); foreach (var f in forUpdate) { f.UserName = authData.Login ?? ""; f.Password = EncryptPassword(authData.Password); f.Token = EncryptPassword(authData.Token ?? ""); f.Url = authData.Url ?? ""; } FilesDbContext.SaveChanges(); return(forUpdate.Count == 1 ? linkId : default);
private void UpdateNewTagsInDb(Tag tag, DateTime createOn) { if (tag == null) return; var forUpdate = Query(FilesDbContext.TagLink) .Where(r => r.TagId == tag.Id) .Where(r => r.EntryType == tag.EntryType) .Where(r => r.EntryId == MappingID(tag.EntryId).ToString()); foreach (var f in forUpdate) { f.CreateBy = AuthContext.CurrentAccount.ID; f.CreateOn = createOn; f.TagCount = tag.Count; } FilesDbContext.SaveChanges(); }
public void DeleteFolder(string folderId) { var folder = GetFolderById(folderId); var id = MakeId(folder); using (var tx = FilesDbContext.Database.BeginTransaction()) { var hashIDs = Query(FilesDbContext.ThirdpartyIdMapping) .Where(r => r.Id.StartsWith(id)) .Select(r => r.HashId) .ToList(); var link = Query(FilesDbContext.TagLink) .Where(r => hashIDs.Any(h => h == r.EntryId)) .ToList(); FilesDbContext.TagLink.RemoveRange(link); FilesDbContext.SaveChanges(); var tagsToRemove = Query(FilesDbContext.Tag) .Where(r => !Query(FilesDbContext.TagLink).Where(a => a.TagId == r.Id).Any()); FilesDbContext.Tag.RemoveRange(tagsToRemove); var securityToDelete = Query(FilesDbContext.Security) .Where(r => hashIDs.Any(h => h == r.EntryId)); FilesDbContext.Security.RemoveRange(securityToDelete); FilesDbContext.SaveChanges(); var mappingToDelete = Query(FilesDbContext.ThirdpartyIdMapping) .Where(r => hashIDs.Any(h => h == r.HashId)); FilesDbContext.ThirdpartyIdMapping.RemoveRange(mappingToDelete); FilesDbContext.SaveChanges(); tx.Commit(); } if (!(folder is ErrorEntry)) { ProviderInfo.Storage.DeleteFileSystemEntry(folder); } }
protected void UploadButton_Click(object sender, EventArgs e) { var db = new FilesDbContext(); foreach (UploadedFile file in this.RadAsyncUpload.UploadedFiles) { if (!file.FileName.EndsWith(".zip")) { continue; } using (var zipFile = ZipFile.Read(file.InputStream)) { foreach (var zipContent in zipFile) { if (!zipContent.FileName.EndsWith(".txt")) { continue; } using (var stream = new MemoryStream()) { zipContent.Extract(stream); stream.Position = 0; var reader = new StreamReader(stream); var txt = reader.ReadToEnd(); db.TextFiles.Add(new TextFile() { Name = zipContent.FileName, Content = txt }); db.SaveChanges(); } } } } }
protected void GetRecalculateFilesCountUpdate(object folderId) { var folders = FilesDbContext.Folders .Where(r => r.TenantId == TenantID) .Where(r => FilesDbContext.Tree.Where(r => r.FolderId.ToString() == folderId.ToString()).Select(r => r.ParentId).Any(a => a == r.Id)) .ToList(); foreach (var f in folders) { var filesCount = FilesDbContext.Files .Join(FilesDbContext.Tree, a => a.FolderId, b => b.FolderId, (file, tree) => new { file, tree }) .Where(r => r.file.TenantId == f.TenantId) .Where(r => r.tree.ParentId == f.Id) .Count(); f.FilesCount = filesCount; } FilesDbContext.SaveChanges(); }
public void Save() { db.SaveChanges(); }
public void SetShare(FileShareRecord r) { if (r.Share == FileShare.None) { var entryId = (MappingID(r.EntryId) ?? "").ToString(); if (string.IsNullOrEmpty(entryId)) { return; } using (var tx = FilesDbContext.Database.BeginTransaction()) { var files = new List <string>(); if (r.EntryType == FileEntryType.Folder) { var folders = new List <string>(); if (int.TryParse(entryId, out var intEntryId)) { var foldersInt = FilesDbContext.Tree .Where(r => r.ParentId.ToString() == entryId) .Select(r => r.FolderId) .ToList(); folders.AddRange(foldersInt.Select(folderInt => folderInt.ToString())); files.AddRange(Query(FilesDbContext.Files).Where(r => foldersInt.Any(a => a == r.FolderId)).Select(r => r.Id.ToString())); } else { folders.Add(entryId); } var toDelete = FilesDbContext.Security .Where(a => a.TenantId == r.Tenant) .Where(a => folders.Any(b => b == a.EntryId)) .Where(a => a.EntryType == FileEntryType.Folder) .Where(a => a.Subject == r.Subject); FilesDbContext.Security.RemoveRange(toDelete); FilesDbContext.SaveChanges(); } else { files.Add(entryId); } if (0 < files.Count) { var toDelete = FilesDbContext.Security .Where(a => a.TenantId == r.Tenant) .Where(a => files.Any(b => b == a.EntryId)) .Where(a => a.EntryType == FileEntryType.File) .Where(a => a.Subject == r.Subject); FilesDbContext.Security.RemoveRange(toDelete); FilesDbContext.SaveChanges(); } tx.Commit(); } } else { var toInsert = new DbFilesSecurity { TenantId = r.Tenant, EntryId = MappingID(r.EntryId, true).ToString(), EntryType = r.EntryType, Subject = r.Subject, Owner = r.Owner, Security = r.Share, TimeStamp = DateTime.UtcNow }; FilesDbContext.AddOrUpdate(r => r.Security, toInsert); FilesDbContext.SaveChanges(); } }