/// <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; } } }
/// <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); } } }