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