Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b9662ddd4c
Fetching contributors…

Cannot retrieve contributors at this time

file 253 lines (225 sloc) 9.768 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
using System;
using System.IO;
using MongoDB;

namespace MongoDB.GridFS
{
    /// <summary>
    ///
    /// </summary>
    public class GridFile{

        private IMongoDatabase db;
        
        private string name;
        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name {
            get { return name; }
        }

        private IMongoCollection files;
        /// <summary>
        /// Gets the files.
        /// </summary>
        /// <value>The files.</value>
        public IMongoCollection Files
        {
            get { return this.files; }
        }

        private IMongoCollection chunks;
        /// <summary>
        /// Gets the chunks.
        /// </summary>
        /// <value>The chunks.</value>
        public IMongoCollection Chunks
        {
            get { return this.chunks; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="GridFile"/> class.
        /// </summary>
        /// <param name="db">The db.</param>
        public GridFile(IMongoDatabase db):this(db,"fs"){}

        /// <summary>
        /// Initializes a new instance of the <see cref="GridFile"/> class.
        /// </summary>
        /// <param name="db">The db.</param>
        /// <param name="bucket">The bucket.</param>
        public GridFile(IMongoDatabase db, string bucket){
            this.db = db;
            this.files = db[bucket + ".files"];
            this.chunks = db[bucket + ".chunks"];
            this.chunks.Metadata.CreateIndex(new Document().Add("files_id", 1).Add("n", 1), true);
            this.files.Metadata.CreateIndex(new Document().Add("filename", 1).Add("n", 1), false);
            this.name = bucket;
        }

        /// <summary>
        /// Lists the files.
        /// </summary>
        /// <returns></returns>
        public ICursor ListFiles(){
            return this.ListFiles(new Document());
        }

        /// <summary>
        /// Lists the files.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <returns></returns>
        public ICursor ListFiles(Document query)
        {
            return this.files.Find(new Document().Add("query", query)
                                                .Add("orderby", new Document()
                                                .Add("filename", 1)));
        }
        
        /// <summary>
        /// Copies one file to another. The destination file must not exist or an IOException will be thrown.
        /// </summary>
        /// <exception cref="FileNotFoundException">Source file not found.</exception>
        /// <exception cref="IOException">Destination file already exists.</exception>
        /// <exception cref="MongoCommandException">A database error occurred executing the copy function.</exception>
        public void Copy(String src, String dest){
            if(Exists(src) == false) throw new FileNotFoundException("Not found in the database.", src);
            if(Exists(dest) == true) throw new IOException("Destination file already exists.");

            Document scope = new Document().Add("bucket", this.name).Add("srcfile", src).Add("destfile", dest);
            String func ="function(){\n" +
                            //" print(\"copying \" + srcfile);\n" +
                            " var files = db[bucket + \".files\"];\n" +
                            " var chunks = db[bucket + \".chunks\"];\n" +
                            " var srcdoc = files.findOne({filename:srcfile});\n" +
                            //" return srcdoc; \n" +
                            " if(srcdoc != undefined){\n" +
                            " var srcid = srcdoc._id;\n" +
                            " var newid = ObjectId();\n" +
                            " srcdoc._id = newid\n" +
                            " srcdoc.filename = destfile;\n" +
                            " files.insert(srcdoc);\n" +
                            " chunks.find({files_id:srcid}).forEach(function(chunk){\n" +
                            //" print(\"copying chunk...\");\n" +
                            " chunk._id = ObjectId();\n" +
                            " chunk.files_id = newid;\n" +
                            " chunks.insert(chunk);\n" +
                            " });\n" +
                            " return true;\n" +
                            " }\n" +
                            " return false;\n" +
                            "}";
            db.Eval(func,scope);
        }
        
        #region Create
        /// <summary>
        /// Creates the specified filename.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <returns></returns>
        public GridFileStream Create(String filename){
            return Create(filename, FileMode.Create);
        }

        /// <summary>
        /// Creates the specified filename.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="mode">The mode.</param>
        /// <returns></returns>
        public GridFileStream Create(String filename, FileMode mode){
            return Create(filename,mode,FileAccess.ReadWrite);
        }

        /// <summary>
        /// Creates the specified filename.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="access">The access.</param>
        /// <returns></returns>
        public GridFileStream Create(String filename, FileMode mode, FileAccess access){
            //Create is delegated to a GridFileInfo because the stream needs access to the gfi and it
            //is easier to do it this way and only write the implementation once.
            GridFileInfo gfi = new GridFileInfo(this.db,this.name,filename);
            return gfi.Create(mode,access);

        }
        #endregion

        #region Opens
        /// <summary>
        /// Opens the specified filename.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="access">The access.</param>
        /// <returns></returns>
        public GridFileStream Open(string filename, FileMode mode, FileAccess access){
            return new GridFileInfo(this.db, this.name, filename).Open(mode, access);
        }

        /// <summary>
        /// Opens the read.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <returns></returns>
        public GridFileStream OpenRead(String filename){
            GridFileInfo gfi = new GridFileInfo(this.db, this.name, filename);
            return gfi.OpenRead();
        }

        /// <summary>
        /// Opens the write.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <returns></returns>
        public GridFileStream OpenWrite(String filename){
            GridFileInfo gfi = new GridFileInfo(this.db, this.name, filename);
            return gfi.OpenWrite();
        }
        #endregion

        
        #region Delete
        
        /// <summary>
        /// Permanently removes a file from the database.
        /// </summary>
        public void Delete(Object id){
            files.Remove(new Document().Add("_id", id));
            chunks.Remove(new Document().Add("files_id", id));
        }
        
        /// <summary>
        /// Permanently removes a file from the database.
        /// </summary>
        public void Delete(String filename){
            this.Delete(new Document().Add("filename", filename));
        }
        
        /// <summary>
        /// Permanently removes all files from the database that match the query.
        /// </summary>
        public void Delete(Document query ){
            foreach(Document doc in ListFiles(query).Documents){
                Delete((Oid)doc["_id"]);
            }
        }
        #endregion
        
        #region Exists
        /// <summary>
        /// Gets a value indicating whether the file exists.
        /// </summary>
        public Boolean Exists(string name){
            return this.files.FindOne(new Document().Add("filename", name)) != null;
        }
        /// <summary>
        /// Gets a value indicating whether the file exists.
        /// </summary>
        public Boolean Exists(Object id){
            return this.files.FindOne(new Document().Add("_id", id)) != null;
        }
        #endregion
        
        #region Move
        /// <summary>
        /// Moves the specified SRC.
        /// </summary>
        /// <param name="src">The SRC.</param>
        /// <param name="dest">The dest.</param>
        public void Move(String src, String dest){
            this.files.Update(new Document().Add("$set", new Document().Add("filename", dest)), new Document().Add("filename", src));
        }

        /// <summary>
        /// Moves the specified id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="dest">The dest.</param>
        public void Move(Object id, String dest){
            this.files.Update(new Document().Add("$set", new Document().Add("filename", dest)), new Document().Add("_id", id));
        }
        #endregion
        
    }

}
Something went wrong with that request. Please try again.