Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 60ff76c

Browse filesBrowse files
Samuel CorderSamuel Corder
Samuel Corder
authored and
Samuel Corder
committed
Added md5 calculation on Close().
Removed some dead code.
1 parent 69ff439 commit 60ff76c
Copy full SHA for 60ff76c

File tree

3 files changed

+27
-12
lines changed
Filter options

3 files changed

+27
-12
lines changed

‎MongoDB.Driver.GridFS/GridFile.cs

Copy file name to clipboardExpand all lines: MongoDB.Driver.GridFS/GridFile.cs
+1-4Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
namespace MongoDB.Driver.GridFS
77
{
88
public class GridFile{
9-
10-
private const int DEFAULT_CHUNKSIZE = 256 * 1024;
11-
private const string DEFAULT_CONTENT_TYPE = "text/plain";
12-
9+
1310
private Database db;
1411

1512
private string name;

‎MongoDB.Driver.GridFS/GridFileInfo.cs

Copy file name to clipboardExpand all lines: MongoDB.Driver.GridFS/GridFileInfo.cs
+24-4Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ namespace MongoDB.Driver.GridFS
99
/// Provides instance methods for the creation, copying, deletion, moving, and opening of files,
1010
/// and aids in the creation of GridFileStream objects. It also contains
1111
/// </summary>
12-
public sealed class GridFileInfo
12+
public class GridFileInfo
1313
{
1414
private const int DEFAULT_CHUNKSIZE = 256 * 1024;
1515
private const string DEFAULT_CONTENT_TYPE = "text/plain";
1616

1717
private GridFile gridFile;
18+
private Database db;
19+
private string bucket;
1820

1921

2022
#region "filedata properties"
@@ -67,13 +69,17 @@ public string Md5{
6769
#endregion
6870

6971
public GridFileInfo(Database db, string bucket, string filename){
72+
this.db = db;
73+
this.bucket = bucket;
7074
this.gridFile = new GridFile(db,bucket);
7175
SetFileDataDefaults(filename);
7276
this.ContentType = DEFAULT_CONTENT_TYPE;
7377
if(gridFile.Exists(filename)) this.LoadFileData();
7478
}
7579

7680
public GridFileInfo(Database db, string filename){
81+
this.db = db;
82+
this.bucket = "fs";
7783
this.gridFile = new GridFile(db);
7884
SetFileDataDefaults(filename);
7985
if(gridFile.Exists(filename)) this.LoadFileData();
@@ -139,12 +145,21 @@ public GridFileStream Open(FileMode mode, FileAccess access){
139145
case FileMode.Create:
140146
case FileMode.CreateNew:
141147
return this.Create(mode, access);
148+
case FileMode.Open:
149+
LoadFileData();
150+
return new GridFileStream(this,this.gridFile.Files, this.gridFile.Chunks, access);
151+
case FileMode.OpenOrCreate:
152+
if(gridFile.Exists(this.FileName) == false) return this.Create(mode, access);
153+
LoadFileData();
154+
return new GridFileStream(this, this.gridFile.Files, this.gridFile.Chunks, access);
142155
case FileMode.Truncate:
143156
this.Truncate();
144157
return new GridFileStream(this,this.gridFile.Files, this.gridFile.Chunks, access);
145-
case FileMode.Open:
158+
case FileMode.Append:
146159
LoadFileData();
147-
return new GridFileStream(this,this.gridFile.Files, this.gridFile.Chunks, access);
160+
GridFileStream gfs = new GridFileStream(this,this.gridFile.Files, this.gridFile.Chunks, access);
161+
gfs.Seek(0,SeekOrigin.End);
162+
return gfs;
148163
}
149164
throw new NotImplementedException("Not all modes are implemented yet.");
150165
}
@@ -167,7 +182,12 @@ public void Truncate(){
167182
this.Length = 0;
168183
this.gridFile.Files.Update(filedata);
169184
}
170-
185+
186+
public string CalcMD5(){
187+
Document doc = this.db.SendCommand(new Document().Append("filemd5", this.Id).Append("root",this.bucket));
188+
return (String)doc["md5"];
189+
}
190+
171191
private void LoadFileData(){
172192
Document doc = this.gridFile.Files.FindOne(new Document().Append("filename",this.FileName));
173193
if(doc != null){

‎MongoDB.Driver.GridFS/GridFileStream.cs

Copy file name to clipboardExpand all lines: MongoDB.Driver.GridFS/GridFileStream.cs
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ namespace MongoDB.Driver.GridFS
1212
/// </summary>
1313
public class GridFileStream : Stream
1414
{
15-
// public static String CHUNKID = "_id";
16-
// public static String CHUNKFILESID = "files_id";
17-
// public static String CHUNKN = "n";
18-
// public static String CHUNKDATA = "data";
1915

2016
private IMongoCollection files;
2117
private IMongoCollection chunks;
@@ -279,6 +275,8 @@ public override void Close(){
279275
this.Flush();
280276
//Should update more gridFileInfo statistics.
281277
gridFileInfo.Length = highestPosWritten;
278+
string md5 = gridFileInfo.CalcMD5();
279+
gridFileInfo.Md5 = md5;
282280
this.files.Update(gridFileInfo.ToDocument());
283281
base.Close();
284282
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.