Beispiel #1
0
 public static Chunk CreateNew(Oid fileId, int number, byte[] data)
 {
     OidGenerator generator = new OidGenerator();
     Chunk chunk = new Chunk();
     chunk.Oid = generator.Generate();
     chunk.Data = data;
     chunk.FileID = fileId;
     chunk.Number = number;
     return chunk;
 }
Beispiel #2
0
 public static Chunk CreateFromDocument(Document document)
 {
     if (document != null && document.Contains("files_id") && document.Contains("data") && document.Contains("n"))
     {
         Chunk chunk = new Chunk();
         chunk.Oid = (MongoDB.Driver.Oid)document["_id"];
         chunk.FileID = (MongoDB.Driver.Oid)document["files_id"];
         chunk.Number = Convert.ToInt32(document["n"]);
         chunk.Data = (byte[])document["data"];
         return chunk;
     }
     else
     {
         throw new ArgumentException("Documento non valido per creare un Chunk");
     }
 }
Beispiel #3
0
        private void ChunkFile(byte[] data, string mongoname, bool overwrite, string contentType)
        {
            if (string.IsNullOrEmpty(contentType))
            {
                contentType = "file/undef";
                if (mongoname.LastIndexOf('.') > 0)
                {
                    contentType = "file/" + mongoname.Substring(mongoname.LastIndexOf('.') + 1);
                }
            }

            _chunksDocument.Clear();
            System.Security.Cryptography.MD5 sscMD5 = System.Security.Cryptography.MD5.Create();
            Document file = null;
            bool closeConnection = false;
            try
            {
                if (this.mongo.Connection.State != ConnectionState.Opened)
                {
                    this.mongo.Connection.Open();
                    closeConnection = true;
                }

                file = files.FindOne(this.FileDocument);
                if (file != null)
                {
                    if (overwrite)
                    {
                        file["length"] = data.Length;
                        file["contentType"] = contentType;
                        file["uploadDate"] = DateTime.Now;
                        file["chunkSize"] = this.ChunkSize;

                        byte[] mHash = sscMD5.ComputeHash(data);

                        file["md5"] = Convert.ToBase64String(mHash);

                        files.Update(file);

                        Document eraser = new Document();
                        eraser.Add("files_id", file["_id"]);
                        foreach (Document erased in chunks.Find(eraser).Documents)
                        {
                            chunks.Delete(erased);
                        }

                        if (data.Length <= this.ChunkSize)
                        {
                            Chunk chunk = new Chunk();
                            chunk.FileID = this.Oid;
                            chunk.Number = 0;
                            chunk.Data = data;
                            _chunksDocument.Add(chunk);
                        }
                        else
                        {
                            int chucksNumbers = data.Length / this.ChunkSize + (data.Length % this.ChunkSize > 0 ? 1 : 0);
                            for (int i = 0; i < chucksNumbers; i++)
                            {
                                byte[] buffer = new byte[i < chucksNumbers - 1 ? this.ChunkSize : data.Length % this.ChunkSize];
                                Array.Copy(data, i * this.ChunkSize, buffer, 0, buffer.Length);

                                Chunk chunk = new Chunk();
                                chunk.FileID = this.Oid;
                                chunk.Number = i;
                                chunk.Data = buffer;
                                _chunksDocument.Add(chunk);
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("Il file '" + file["filename"] + " esiste nella collection '" + this.files.FullName + "'");
                    }
                }
                else
                {
                    file = new Document();
                    OidGenerator oidg = new OidGenerator();
                    file.Add("_id", oidg.Generate());
                    file.Add("contentType", contentType);
                    file.Add("length", data.Length);
                    file.Add("chunkSize", DEFAULT_CHUNKSIZE);
                    file.Add("uploadDate", DateTime.Now);

                    byte[] mHash = sscMD5.ComputeHash(data);

                    file.Add("md5", Convert.ToBase64String(mHash));

                    files.Insert(file);

                    if (data.Length <= this.ChunkSize)
                    {
                        Chunk chunk = new Chunk();
                        chunk.FileID = this.Oid;
                        chunk.Number = 0;
                        chunk.Data = data;
                        _chunksDocument.Add(chunk);
                    }
                    else
                    {
                        int chucksNumbers = data.Length / this.ChunkSize + (data.Length % this.ChunkSize > 0 ? 1 : 0);
                        for (int i = 0; i < chucksNumbers; i++)
                        {
                            byte[] buffer = new byte[i < chucksNumbers - 1 ? this.ChunkSize : data.Length % this.ChunkSize];
                            Array.Copy(data, i * this.ChunkSize, buffer, 0, buffer.Length);

                            Chunk chunk = new Chunk();
                            chunk.FileID = this.Oid;
                            chunk.Number = i;
                            chunk.Data = buffer;
                            _chunksDocument.Add(chunk);
                        }
                    }
                }

                this.CreateIndex();
                foreach (Chunk c in _chunksDocument)
                {
                    chunks.Insert(c.Document);
                }
                this.FileDocument = file;

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (closeConnection)
                {
                    this.mongo.Connection.Close();
                }
            }
        }