public void Create(TModel model, byte[] file)
        {
            ObjectId fileInfo = gridFs.UploadFromBytes(model.FileName, file);

            model.FileId     = fileInfo;
            model.InsertTime = DateTime.Now;
            mongoCollection.InsertOne(model);
        }
Exemplo n.º 2
0
        public IStorageFile CreateFile(string path, byte[] arr = null)
        {
            if (arr == null)
            {
                arr = new byte[0];
            }
            var id   = _bucket.UploadFromBytes(path, arr);
            var info = _bucket.Find(Builders <GridFSFileInfo> .Filter.Eq("_id", id)).First();

            return(new GridFSFileStorage(info, _bucket));
        }
Exemplo n.º 3
0
        public ObjectId UploadFile(string filename, IDictionary <string, List <string> > MetaDataPair, byte[] bytea, string bucketName)
        {
            Bucket = new GridFSBucket(mongoDatabase, new GridFSBucketOptions
            {
                BucketName     = bucketName,
                ChunkSizeBytes = 1048576, // 1MB
                WriteConcern   = WriteConcern.WMajority,
                ReadPreference = ReadPreference.Secondary
            });

            this.Metadata = new BsonDocument();

            if (MetaDataPair != null)
            {
                this.Metadata.AddRange(MetaDataPair as IDictionary);
            }

            var options = new GridFSUploadOptions
            {
                ChunkSizeBytes = 64512, // 63KB
                Metadata       = Metadata
            };

            try
            {
                return(Bucket.UploadFromBytes(filename, bytea, options));
            }
            catch (Exception e)
            {
                return(new ObjectId("Error"));
            }
        }
Exemplo n.º 4
0
        private ObjectId UpdateEmployeePicture(Guid employeeId, byte[] picture)
        {
            FilterDefinition <GridFSFileInfo> filter = CreateEmployeePictureFilter(employeeId);
            GridFSFileInfo oldPictureInfo            = binaryFiles.Find(filter).SingleOrDefault();

            if (picture == null)
            {
                return(oldPictureInfo?.Id ?? ObjectId.Empty);
            }
            if (oldPictureInfo != null)
            {
                binaryFiles.Delete(oldPictureInfo.Id);
            }

            return(binaryFiles.UploadFromBytes(employeeId.ToString(), picture));
        }
        public string UploadFile(string filename, byte[] bytea, EbFileCategory cat)
        {
            Bucket = new GridFSBucket(mongoDatabase, new GridFSBucketOptions
            {
                BucketName     = cat.ToString(),
                ChunkSizeBytes = 1048576, // 1MB
                WriteConcern   = WriteConcern.WMajority,
                ReadPreference = ReadPreference.Secondary
            });

            var options = new GridFSUploadOptions
            {
                ChunkSizeBytes = 64512, // 63KB
                Metadata       = Metadata
            };

            try
            {
                return(Bucket.UploadFromBytes(filename, bytea, options).ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception:" + e.ToString());
                return("Error");
            }
        }
Exemplo n.º 6
0
        public void EmployeeWithPictureIsAdded()
        {
            byte[]   picture   = new byte[0];
            Guid     id        = Guid.NewGuid();
            ObjectId pictureId = new ObjectId();

            binaryFiles.UploadFromBytes(id.ToString(), picture).Returns(pictureId);

            sut.AddEmployee(id, "LeBlanc", "Matt", "comedian", "Madrid", "bosses", string.Empty, picture);

            employees.Received(1).InsertOne(Arg.Is <Employee>(e => e.PictureFileId.Equals(pictureId)));
        }
        public OperationResult Execute(CancellationToken cancellationToken)
        {
            try
            {
                var result = _bucket.UploadFromBytes(_filename, _source, _options, cancellationToken);

                return(OperationResult.FromResult(result));
            }
            catch (Exception exception)
            {
                return(OperationResult.FromException(exception));
            }
        }
Exemplo n.º 8
0
        public IStorageFolder GetParent()
        {
            var pathParts = _fileInfo.Filename.TrimEnd('\\').Split('\\');
            var result    = string.Empty;

            for (var i = 0; i < pathParts.Length - 1; i++)
            {
                result += pathParts[i] + "\\";
            }
            var files      = _bucket.Find(Builders <GridFSFileInfo> .Filter.Empty).ToList();
            var fileResult = files.FirstOrDefault(x => x.Filename == result);

            if (fileResult == null)
            {
                var arr = new byte[0];
                result = result.TrimEnd('\\') + '\\';
                var id = _bucket.UploadFromBytes(result, arr);
                fileResult = _bucket.Find(Builders <GridFSFileInfo> .Filter.Eq("_id", id)).First();
            }
            return(new GridFSFolderStorage(_bucket, fileResult));
        }
Exemplo n.º 9
0
        public ObjectId saveSRTBucket(string srtPath)
        {
            using (var stream = File.OpenRead(srtPath))
            {
                FormFile formFile = new FormFile(stream, 0, stream.Length, "SrtFile", Path.GetFileName(stream.Name))
                {
                    Headers     = new HeaderDictionary(),
                    ContentType = "text/srt"
                };

                //get the bytes from the content stream of the file
                byte[] theFileAsBytes = new byte[formFile.Length];
                using (BinaryReader theReader = new BinaryReader(formFile.OpenReadStream()))
                {
                    theFileAsBytes = theReader.ReadBytes((int)formFile.Length);
                }

                //כיון ששמירת קבצים במונגו נשמר באחסון GridFSBucket  בתוך המונגו
                ObjectId srtFile = bucket.UploadFromBytes(formFile.FileName, theFileAsBytes);

                return(srtFile);
            }
        }
Exemplo n.º 10
0
 private void EnsureBucketExists(IGridFSBucket bucket)
 {
     bucket.UploadFromBytes("filename", new byte[0]);
 }
 private ObjectId CreateGridFSFile(IGridFSBucket bucket, byte[] content)
 {
     return bucket.UploadFromBytes("filename", content);
 }
Exemplo n.º 12
0
 private ObjectId CreateGridFSFile(IGridFSBucket bucket, byte[] content)
 {
     return(bucket.UploadFromBytes("filename", content));
 }
 private void EnsureBucketExists(IGridFSBucket bucket)
 {
     bucket.UploadFromBytes("filename", new byte[0]);
 }
Exemplo n.º 14
0
 public ObjectId UploadFromBytes(string filename, byte[] source) =>
 _bucket.UploadFromBytes(filename, source);