public DocumentHandler(DataClasses.Document document) { if (document.Mode == "initial") { Import_Staging(document); } }
private void Import_Staging(DataClasses.Document document) { PuttyMigrateXSDTableAdapters.StagingTableAdapter da = new PuttyMigrateXSDTableAdapters.StagingTableAdapter(); PuttyMigrateXSD.StagingDataTable dt = new PuttyMigrateXSD.StagingDataTable(); try { da.Insert_StagingTable(document.SharePoint_FileId1, document.SharePoint_FileId2, document.SharePoint_FileId3, document.SharePoint_FileId4, document.SharePoint_FileId5, document.Mongo_FileId1, document.Mongo_FileId2, document.Mongo_FileId3, document.Mongo_FileId4, document.Mongo_FileId5, document.SharePoint_Meta, document.Mongo_Meta, document.Content_Type, document.Content_Ext, document.Content_Bytes, document.Content_Size, document.UploadFilePath, document.DownloadFilePath, document.Content_title, document.Content_Date, document.Import_Date); } catch (Exception ex) { //add to a new logging table LogError("Import Staging: " + ex.Message.ToString(), "error"); } }
public bool SaveBinary(byte[] content, int numDocs, DataClasses.Document document) { bool nResult = true; var collection = db.database.GetCollection("document"); BsonDocument baseDoc = new BsonDocument(); baseDoc.SetElement(new BsonElement("pdfContent", content)); for (int i = 0; i < numDocs; ++i) { var guid = Guid.NewGuid(); baseDoc.SetElement(new BsonElement("_id", guid)); baseDoc.SetElement(new BsonElement("filename", document.UploadFilePath)); baseDoc.SetElement(new BsonElement("title", document.DocumentTitle)); collection.Insert(baseDoc); document.FileId = guid.ToString(); document.DocumentId = GetOneDocumentById(guid.ToString()); // InsertMongoDBLink(document); } return(nResult); }
//private void RetrieveFromGridBucketFS() //{ // SetupMongoDb(); // var bucket = new GridFSBucket(mongoDb, new GridFSBucketOptions // { // BucketName = "pdfs", // }); // var filesIds = mongoDb.GetCollection<DataClasses.Document>("pictures.files").Find(new BsonDocument()).ToEnumerable().Select(doc => doc.GetElement("_id").Value); // foreach (var id in filesIds) // { // var fileBytes = bucket.DownloadAsBytes(id); // fileBytes = null; // } //} public DataClasses.Document SaveFilesToGridFSBinary(int numFiles, byte[] content, string fileName, DataClasses.Document document) { SetupMongoDb(); fileName = document.UploadFilePath; string MongoFileId = string.Empty; var bucket = new GridFSBucket(mongoDb, new GridFSBucketOptions { BucketName = document.BucketName }); for (int i = 0; i < numFiles; ++i) { string targetFileName = $"{fileName.Substring(0, fileName.Length - ".pdf".Length)}{i}.pdf"; int chunkSize = content.Length <= 1048576 ? 51200 : 1048576; var id = bucket.UploadFromBytes(targetFileName, content, new GridFSUploadOptions { ChunkSizeBytes = chunkSize }); document.FileId = ""; document.DocumentId = id.ToString(); //InsertMongoDBLink(document); } return(document); }
public DataClasses.Document GetOneDocumentDetailByFileId(DataClasses.Document document) { string fileContents = string.Empty; ObjectId obj = new ObjectId(document.FileId); document.Entity = ""; string location = string.Empty; try { location = System.Configuration.ConfigurationManager.AppSettings["DownloadLocation"].ToString(); } catch { location = @"C:\Temp\"; } var query = Query.EQ("_id", obj); var file = db.database.GridFS.FindOne(query); string name = file.Name; var newFileName = name; try { Array arrSplit = name.Split('\\'); int intlen = arrSplit.Length; string filename = arrSplit.GetValue(intlen - 1).ToString(); newFileName = location + filename; document.DocumentTitle = "Your file is located here: " + newFileName; } catch (Exception ex) { document.DocumentTitle = "Unable to handle the file name - section 001"; document.Entity = string.Empty; ErrorMessage = ex.Message.ToString(); } try { using (var stream = file.OpenRead()) { var bytes = new byte[stream.Length]; stream.Read(bytes, 0, (int)stream.Length); using (var newFs = new FileStream(newFileName, FileMode.Create)) { newFs.Write(bytes, 0, bytes.Length); } } document.nResult = true; document.Entity = newFileName; } catch (Exception ex) { document.nResult = false; document.DocumentTitle = "Unable to download this document (original file name and location: " + name; document.Entity = string.Empty; ErrorMessage = ex.Message.ToString(); } //using (var fs = file.OpenRead()) //{ // var bytes = new byte[fs.Length]; // fs.Read(bytes, 0, (int)fs.Length); // var stream = new MemoryStream(bytes); // // var res = Encoding.UTF8.GetString(stream.GetBuffer(), 0, stream.GetBuffer().Length); // StreamReader reader = new StreamReader(stream); // string text = reader.ReadToEnd(); // fileContents = text; //} return(document); }