示例#1
0
        public async Task <string> CreateAsync(string hallId, string standId, ExhibitViewModel entity)
        {
            if (entity.Photos != null)
            {
                foreach (var photo in entity.Photos)
                {
                    ObjectId id = await _gridFS.UploadFromBytesAsync(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff"), photo?.Photo);

                    photo.Id    = id.ToString();
                    photo.Photo = null;
                }
            }
            else
            {
                entity.Photos = new List <PhotoInfo>();
            }

            var filter = Builders <HallViewModel> .Filter.And(
                Builders <HallViewModel> .Filter.Where(hall => hall.Id.Equals(hallId)),
                Builders <HallViewModel> .Filter.Eq("Stands.Id", standId));

            entity.Id = ObjectId.GenerateNewId().ToString();
            var update = Builders <HallViewModel> .Update.Push("Stands.$.Exhibits", entity);

            await _halls.FindOneAndUpdateAsync(filter, update);

            return(entity.Id);
        }
示例#2
0
        public async Task <string> CreateAsync(string hallId, Stand stand)
        {
            var hall = await _halls.Find(hall => hall.Id.Equals(hallId)).FirstOrDefaultAsync();

            if (hall != null)
            {
                if (stand.Photo?.Photo != null)
                {
                    ObjectId id = await _gridFS.UploadFromBytesAsync(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff"), stand.Photo.Photo);

                    stand.Photo.Id    = id.ToString();
                    stand.Photo.Photo = null;
                }

                stand.Id = ObjectId.GenerateNewId().ToString();
                var filter = Builders <Hall> .Filter.Eq("Id", hallId);

                var update = Builders <Hall> .Update.Push("Stands", stand);

                await _halls.UpdateOneAsync(filter, update);

                return(stand.Id);
            }
            return(null);
        }
示例#3
0
        public async Task SeedIntialDataAsync()
        {
            Model.Media media = DefaultMedia;
            IEnumerable <Model.MediaFace> faces = GetDefaultFaces(media.Id).ToList();

            await _mediaStoreContext.Medias.InsertOneAsync(media);

            await _mediaStoreContext.Cameras.InsertOneAsync(DefaultCamera);

            await _mediaStoreContext.Persons.InsertOneAsync(DefaultPerson);

            await _mediaStoreContext.Faces.InsertManyAsync(
                GetDefaultFaces(DefaultMedia.Id));

            IGridFSBucket gridFs = _mediaStoreContext.CreateGridFsBucket();

            foreach (Model.MediaThumbnail thumb in media.Thumbnails)
            {
                await gridFs.UploadFromBytesAsync(thumb.Id.ToString("N"), thumb.Data);
            }

            foreach (Model.MediaFace face in faces)
            {
                await gridFs.UploadFromBytesAsync(
                    face.Thumbnail.Id.ToString("N"),
                    face.Thumbnail.Data);
            }
        }
        //Async
        public async Task <IStorageFile> CreateFileAsync(string path, byte[] arr = null)
        {
            if (arr == null)
            {
                arr = new byte[0];
            }
            var id = await _bucket.UploadFromBytesAsync(path, arr);

            var info = _bucket.FindAsync(Builders <GridFSFileInfo> .Filter.Eq("_id", id)).Result.First();

            return(new GridFSFileStorage(info, _bucket));
        }
示例#5
0
        public async Task <string> CreateAsync(HallViewModel hall)
        {
            if (hall.Photo?.Photo != null)
            {
                ObjectId id = await _gridFS.UploadFromBytesAsync(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff"), hall.Photo.Photo);

                hall.Photo.Id    = id.ToString();
                hall.Photo.Photo = null;
            }
            hall.Id = ObjectId.GenerateNewId().ToString();
            await _halls.InsertOneAsync(hall);

            return(hall.Id);
        }
示例#6
0
        public async Task StoreImage(ProductPhoto photo, byte[] image)
        {
            try
            {
                Log.Instance.LogAsInfo($"{nameof(ProductPhotoRepository)}.{nameof(StoreImage)}: StoreImage is starting. {nameof(ProductPhoto)}");
                if (photo.HasImageSource())
                {
                    //если ранее уже была фотка то удаляем её
                    photo.ImageSize = "0";
                    await _gridFS.DeleteAsync(new ObjectId(photo.ImageSourceId));
                }

                string   fileName = photo.ProductId;
                ObjectId imageId  = await _gridFS.UploadFromBytesAsync(fileName, image);

                photo.ImageSize = image.Length.ToString();

                //обновляем данные по дуомукументу

                photo.ImageSourceId = imageId.ToString();
                var filter = Builders <ProductPhoto> .Filter.Eq("ProductId", photo.ProductId);

                var update = Builders <ProductPhoto> .Update.Set("ImageSourceId", photo.ImageSourceId).Set("ImageSize", photo.ImageSize);

                await Photos.UpdateOneAsync(filter, update);

                Log.Instance.LogAsInfo($"{nameof(ProductPhotoRepository)}.{nameof(StoreImage)}: StoreImage is successfully ended.");
            }
            catch (Exception e)
            {
                Log.Instance.ExceptionInfo(e).LogAsError($"{nameof(ProductPhotoRepository)}.{nameof(StoreImage)}: Error");
                throw;
            }
        }
示例#7
0
 public async Task StoreAsync(ThumbnailData data, CancellationToken cancellationToken)
 {
     await _gridFSBucket.UploadFromBytesAsync(
         data.Id.ToString("N"),
         data.Data,
         options : null,
         cancellationToken);
 }
示例#8
0
 public async Task UploadFileAsync(string taskId, byte[] data)
 {
     try
     {
         await _gridFs.UploadFromBytesAsync(taskId, data);
     }
     catch (Exception e)
     {
         _logger.LogError("Error occured while uploading {@TaskId} in {@AgentName} with {@Exception}",
                          taskId, nameof(FileWorkerAgentImpl), e.ToString());
     }
 }
        public async Task <ObjectId> uploadFileAsync(string fileName, string mimeType, byte[] fileBytes)
        {
            // var mimeType = sojourner_backend.MimeTypes.GetMimeType(fileName);
            var options = new GridFSUploadOptions()
            {
                Metadata = new ImageMetadata()
                {
                    mimeType = mimeType
                }.ToBsonDocument()
            };

            return(await bucket.UploadFromBytesAsync(fileName, fileBytes, options));
        }
示例#10
0
        public async Task <string> CreateAsync(byte[] data, string fileName, CancellationToken token)
        {
            token.ThrowIfCancellationRequested();

            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var fileId = await files.UploadFromBytesAsync(fileName, data, null, token).ConfigureAwait(false);

            return(fileId.ToString());
        }
        public async Task <OperationResult> ExecuteAsync(CancellationToken cancellationToken)
        {
            try
            {
                var result = await _bucket.UploadFromBytesAsync(_filename, _source, _options, cancellationToken);

                return(OperationResult.FromResult(result));
            }
            catch (Exception exception)
            {
                return(OperationResult.FromException(exception));
            }
        }
示例#12
0
        public async Task <bool> UploadPhotoFile(ObjectId photoId, string fileName, byte[] source)
        {
            var entity = await GetEntityByIdAsync(photoId);

            if (entity != null && !string.IsNullOrWhiteSpace(fileName) && source != null)
            {
                if (entity.FileId != ObjectId.Empty)
                {
                    await _gridFs.DeleteAsync(entity.FileId);
                }
                entity.FileName = fileName;
                entity.FileId   = await _gridFs.UploadFromBytesAsync(fileName, source);

                return(await UpdateEntityAsync(entity));
            }

            return(false);
        }
示例#13
0
        public async Task <IEnumerable <ObjectId> > CreatePhotos(IEnumerable <Photo> photos)
        {
            var result = new List <ObjectId>();

            if (photos != null)
            {
                foreach (var photo in photos)
                {
                    if (!photo.Content.Any())
                    {
                        continue;
                    }
                    ObjectId imageId = await _gridFs.UploadFromBytesAsync(photo.FileName, photo.Content);

                    result.Add(imageId);
                }
            }
            return(result);
        }
示例#14
0
        /// <summary>
        /// Save note to database. Also save photos of the notes separately with GridFS.
        /// </summary>
        /// <param name="note">Note to be saved to database.</param>
        /// <param name="photos">List of photos.</param>
        /// <returns></returns>
        public async Task <Note> CreateNoteAsync(Note note, IEnumerable <Photo> photos = null)
        {
            if (photos != null)
            {
                foreach (var photo in photos)
                {
                    if (!photo.Content.Any())
                    {
                        continue;
                    }
                    ObjectId imageId = await _gridFs.UploadFromBytesAsync(photo.FileName, photo.Content);

                    note.FilesIds?.Add(imageId);
                }
            }

            await _notes.InsertOneAsync(note);

            return(note);
        }
示例#15
0
        public async Task <CdnUploadResult> MongoCdn_UploadFile(CdnUploadRequest request)
        {
            CdnUploadResult result = new CdnUploadResult()
            {
                Succeded = false
            };
            Dictionary <string, object> metadata = new Dictionary <string, object>();

            metadata.Add("contentType", request.ContentType);
            metadata.Add("sourceType", request.SourceType);
            metadata.Add("sourceId", request.SourceId);
            metadata.Add("title", request.Title);

            var options = new GridFSUploadOptions
            {
                Metadata = new BsonDocument(metadata)
            };

            request.FileContent = Convert.FromBase64String(request.FileContentBase64);

            try
            {
                string mongoFileId = (await gridFsBucket.UploadFromBytesAsync(request.FileName, request.FileContent, options)).ToString();

                if (!string.IsNullOrEmpty(mongoFileId))
                {
                    result.Succeded = SaveMongoFileData(request, mongoFileId);
                }

                result.FileId = mongoFileId;
            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;
            }

            return(result);
        }
示例#16
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        public static Task <ObjectId> UploadFromBytes(string fileName, byte[] source)
        {
            //构建查询,用于检查是否已存在
            FilterDefinition <GridFSFileInfo> filter = Builders <GridFSFileInfo> .Filter.And(
                Builders <GridFSFileInfo> .Filter.Eq(x => x.Filename, fileName)
                //Builders<GridFSFileInfo>.Filter.Gte(x => x.UploadDateTime, new DateTime(2015, 1, 1, 0, 0, 0, DateTimeKind.Utc)),
                );

            //排序规则
            SortDefinition <GridFSFileInfo> sort = Builders <GridFSFileInfo> .Sort.Descending(x => x.UploadDateTime);

            //查找限制,提高处理速度
            GridFSFindOptions options = new GridFSFindOptions {
                Limit = 1,
                Sort  = sort
            };

            using (var cursor = bucket.Find(filter, options))
            {
                GridFSFileInfo fileInfo = cursor.ToList().FirstOrDefault();
                return(fileInfo == null?Task.FromResult(bucket.UploadFromBytesAsync(fileName, source).Result) : Task.FromResult(fileInfo.Id));
            }
        }
        public async Task <bool> SaveImage(StorableImage image)
        {
            await buket.UploadFromBytesAsync(image.Name, image.Content);

            return(true);
        }
示例#18
0
        public async Task <bool> UploadFileFromBytesAsync(string fileNameGUID, byte[] fileBytes)
        {
            ObjectId Id = await _gridFSBucket.UploadFromBytesAsync(fileNameGUID, fileBytes);

            return(Id != null);
        }
 public async Task SaveAsync(ImageWrapped item)
 {
     await _bucket
     .UploadFromBytesAsync(item.Filename, item.ImageData)
     .ConfigureAwait(false);
 }