/// <summary> /// Purges the file and all it's data from the system. /// </summary> /// <param name="context"></param> /// <param name="fileId"></param> private static void HardDeleteFile(this OnlineFilesEntities context, Guid fileId) { bool createdContext = false; if (context == null) { createdContext = true; context = new OnlineFilesEntities(); } try { File file = context.Files .Include(c => c.FileSecurities) .Include(c => c.FileDatas.Select(d => d.Catalog)) .FirstOrDefault(x => x.pk_FileId == fileId); if (file == null) { throw new FileNotFoundException("File Not found with: " + fileId); } //Remove the data from the catalog. foreach (FileData data in file.FileDatas) { using (var cfd = new OnlineFiles_CatalogEntities(data.Catalog.EntityConnectionString)) { cfd.FileCatalogEntries.Remove(cfd.FileCatalogEntries.FirstOrDefault(d => d.pk_FileCatalogEntryId == data.fk_ContentId)); cfd.SaveChanges(); } } //Remove Pointer records to data context.FileDatas.RemoveRange(file.FileDatas); //Remove File Security Records. context.FileSecurities.RemoveRange(file.FileSecurities); context.Files.Remove(file); } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } finally { if (createdContext) { context.SaveChanges(); context.Dispose(); context = null; } } }
/// <summary> /// Opens a READ only stream to the file data. /// </summary> /// <param name="user"></param> /// <returns></returns> public Stream OpenReadStream(Principal user) { using (var context = new OnlineFilesEntities()) { var sec = context.FileSecurities.Where(d => d.fk_FileId == pk_FileId && user.UserProfile.mySecurityGroups.Contains(d.SecurityObjectId)); if (!sec.Any(d => d.canRead)) { throw new SecurityException("Not Authorized."); } FileData filedata = context.FileDatas.AsNoTracking() .Include(x => x.Catalog) .Where(d => d.fk_FileId == pk_FileId) .OrderByDescending(d => d.Revision) .FirstOrDefault(); if (filedata == null) { return new MemoryStream(_emptyBytes.ToArray()) { Position = 0 } } ; using (var ctx = new OnlineFiles_CatalogEntities(filedata.Catalog.EntityConnectionString)) { FileCatalogEntry filecat = ctx.FileCatalogEntries.AsNoTracking() .FirstOrDefault(d => d.pk_FileCatalogEntryId == filedata.fk_ContentId); if (filecat == null) { return new MemoryStream(_emptyBytes.ToArray()) { Position = 0 } } ; return(new MemoryStream(filecat.binaryData) { Position = 0 }); } } }
/// <summary> /// Purges the file and all it's data from the system. /// </summary> /// <param name="context"></param> /// <param name="fileId"></param> private static void HardDeleteFile(this OnlineFilesEntities context, Guid fileId) { bool createdContext = false; if (context == null) { createdContext = true; context = new OnlineFilesEntities(); } try { File file = context.Files .Include(c => c.FileSecurities) .Include(c => c.FileDatas.Select(d => d.Catalog)) .FirstOrDefault(x => x.pk_FileId == fileId); if (file == null) throw new FileNotFoundException("File Not found with: " + fileId); //Remove the data from the catalog. foreach (FileData data in file.FileDatas) using (var cfd = new OnlineFiles_CatalogEntities(data.Catalog.EntityConnectionString)) { cfd.FileCatalogEntries.Remove(cfd.FileCatalogEntries.FirstOrDefault(d => d.pk_FileCatalogEntryId == data.fk_ContentId)); cfd.SaveChanges(); } //Remove Pointer records to data context.FileDatas.RemoveRange(file.FileDatas); //Remove File Security Records. context.FileSecurities.RemoveRange(file.FileSecurities); context.Files.Remove(file); } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } finally { if (createdContext) { context.SaveChanges(); context.Dispose(); context = null; } } }
/// <summary> /// Opens a WRITE stream to the file. /// </summary> /// <param name="user"></param> /// <param name="itemPath"></param> /// <param name="webDavSqlStoreDocumentFactoryInstance"></param> /// <returns></returns> public Stream OpenWriteStream(Principal user, string itemPath = null, object webDavSqlStoreDocumentFactoryInstance = null) { using (var context = new OnlineFilesEntities()) { if (!(context.FileSecurities.Where(d => d.fk_FileId == pk_FileId).ToList().Any(x => user.UserProfile.mySecurityGroups.Contains(x.SecurityObjectId) && x.canWrite))) throw new SecurityException("Not Authorized."); int revision = 0; FileData fd = context.FileDatas.Include(x => x.Catalog).Where(d => d.fk_FileId == pk_FileId).OrderByDescending(d => d.Revision).FirstOrDefault(); if (fd != null) revision = fd.Revision; revision++; Catalog catalog; if (fd == null || fd.Catalog.fk_CatalogStatusId != CatalogStatus.Open) { Folder f = context.Folders.FirstOrDefault(d => d.pk_FolderId == fk_FolderId); if (f == null) throw new Exception("Null ptr"); CatalogCollection t = context.CatalogCollections.Include(d => d.Catalogs).FirstOrDefault(d => d.pk_CatalogCollectionId == f.fk_CatalogCollectionId); if (t == null) throw new Exception("Cat col is null"); catalog = t.Catalogs.FirstOrDefault(d => d.fk_CatalogStatusId == CatalogStatus.Open); if (catalog == null) throw new Exception("No Catalog Available."); } else catalog = fd.Catalog; if (catalog == null) throw new Exception("No Catalog Available for file."); using (var ctx = new OnlineFiles_CatalogEntities(catalog.EntityConnectionString)) { FileCatalogEntry fce = new FileCatalogEntry { binaryData = _emptyBytes.ToArray() }; ctx.FileCatalogEntries.Add(fce); ctx.SaveChanges(); FileData filedata = new FileData { fk_FileId = pk_FileId, Revision = revision, Size = 0, CreateDt = DateTime.Now, fk_CatalogId = catalog.pk_CatalogId, fk_ContentId = fce.pk_FileCatalogEntryId }; context.FileDatas.Add(filedata); context.SaveChanges(); Stream stream = new SqlStoreFileStream { CatalogId = catalog.pk_CatalogId, FileCatalogEntryId = fce.pk_FileCatalogEntryId, Path = itemPath, FileDataId = filedata.pk_FileDataId, WebDavSqlStoreDocumentFactoryInstance = webDavSqlStoreDocumentFactoryInstance }; return stream; } } }
/// <summary> /// Opens a READ only stream to the file data. /// </summary> /// <param name="user"></param> /// <returns></returns> public Stream OpenReadStream(Principal user) { using (var context = new OnlineFilesEntities()) { var sec = context.FileSecurities.Where(d => d.fk_FileId == pk_FileId && user.UserProfile.mySecurityGroups.Contains(d.SecurityObjectId)); if (!sec.Any(d => d.canRead)) throw new SecurityException("Not Authorized."); FileData filedata = context.FileDatas.AsNoTracking() .Include(x => x.Catalog) .Where(d => d.fk_FileId == pk_FileId) .OrderByDescending(d => d.Revision) .FirstOrDefault(); if (filedata == null) return new MemoryStream(_emptyBytes.ToArray()) { Position = 0 }; using (var ctx = new OnlineFiles_CatalogEntities(filedata.Catalog.EntityConnectionString)) { FileCatalogEntry filecat = ctx.FileCatalogEntries.AsNoTracking() .FirstOrDefault(d => d.pk_FileCatalogEntryId == filedata.fk_ContentId); if (filecat == null) return new MemoryStream(_emptyBytes.ToArray()) { Position = 0 }; return new MemoryStream(filecat.binaryData) { Position = 0 }; } } }
/// <summary> /// Opens a WRITE stream to the file. /// </summary> /// <param name="user"></param> /// <param name="itemPath"></param> /// <param name="webDavSqlStoreDocumentFactoryInstance"></param> /// <returns></returns> public Stream OpenWriteStream(Principal user, string itemPath = null, object webDavSqlStoreDocumentFactoryInstance = null) { using (var context = new OnlineFilesEntities()) { if (!(context.FileSecurities.Where(d => d.fk_FileId == pk_FileId).ToList().Any(x => user.UserProfile.mySecurityGroups.Contains(x.SecurityObjectId) && x.canWrite))) { throw new SecurityException("Not Authorized."); } int revision = 0; FileData fd = context.FileDatas.Include(x => x.Catalog).Where(d => d.fk_FileId == pk_FileId).OrderByDescending(d => d.Revision).FirstOrDefault(); if (fd != null) { revision = fd.Revision; } revision++; Catalog catalog; if (fd == null || fd.Catalog.fk_CatalogStatusId != CatalogStatus.Open) { Folder f = context.Folders.FirstOrDefault(d => d.pk_FolderId == fk_FolderId); if (f == null) { throw new Exception("Null ptr"); } CatalogCollection t = context.CatalogCollections.Include(d => d.Catalogs).FirstOrDefault(d => d.pk_CatalogCollectionId == f.fk_CatalogCollectionId); if (t == null) { throw new Exception("Cat col is null"); } catalog = t.Catalogs.FirstOrDefault(d => d.fk_CatalogStatusId == CatalogStatus.Open); if (catalog == null) { throw new Exception("No Catalog Available."); } } else { catalog = fd.Catalog; } if (catalog == null) { throw new Exception("No Catalog Available for file."); } using (var ctx = new OnlineFiles_CatalogEntities(catalog.EntityConnectionString)) { FileCatalogEntry fce = new FileCatalogEntry { binaryData = _emptyBytes.ToArray() }; ctx.FileCatalogEntries.Add(fce); ctx.SaveChanges(); FileData filedata = new FileData { fk_FileId = pk_FileId, Revision = revision, Size = 0, CreateDt = DateTime.Now, fk_CatalogId = catalog.pk_CatalogId, fk_ContentId = fce.pk_FileCatalogEntryId }; context.FileDatas.Add(filedata); context.SaveChanges(); Stream stream = new SqlStoreFileStream { CatalogId = catalog.pk_CatalogId, FileCatalogEntryId = fce.pk_FileCatalogEntryId, Path = itemPath, FileDataId = filedata.pk_FileDataId, WebDavSqlStoreDocumentFactoryInstance = webDavSqlStoreDocumentFactoryInstance }; return(stream); } } }