Beispiel #1
0
        public async Task RemoveAsync(string hallId, string id)
        {
            var stand = await GetAsync(hallId, id);

            if (stand.Photo?.Id != null)
            {
                await _gridFS.DeleteAsync(ObjectId.Parse(stand.Photo.Id));
            }
            foreach (var exhibit in stand.Exhibits)
            {
                if (exhibit.Photos != null && exhibit.Photos?.Count != 0)
                {
                    foreach (var photo in exhibit.Photos)
                    {
                        if (photo?.Id != null)
                        {
                            await _gridFS.DeleteAsync(ObjectId.Parse(photo.Id));
                        }
                    }
                }
            }
            var update = Builders <HallViewModel> .Update.PullFilter(hall => hall.Stands,
                                                                     stand => stand.Id.Equals(id));

            var result = await _halls
                         .FindOneAndUpdateAsync(hall => hall.Id.Equals(hallId), update);
        }
Beispiel #2
0
        public async Task DeleteByPrefixAsync(string prefix,
                                              CancellationToken ct = default)
        {
            var name = GetFileName(prefix, nameof(prefix));

            try
            {
                var match = new BsonRegularExpression($"^{name}");

                var fileQuery = await bucket.FindAsync(Filters.Regex(x => x.Id, match), cancellationToken : ct);

                await fileQuery.ForEachAsync(async file =>
                {
                    try
                    {
                        await bucket.DeleteAsync(file.Id, ct);
                    }
                    catch (GridFSFileNotFoundException)
                    {
                        return;
                    }
                }, ct);
            }
            catch (GridFSFileNotFoundException)
            {
                return;
            }
        }
        public async void DeleteFileAsync(string path)
        {
            var files      = _bucket.Find(Builders <GridFSFileInfo> .Filter.Empty).ToList();
            var fileResult = files.FirstOrDefault(x => x.Filename == path);

            if (fileResult != null)
            {
                await _bucket.DeleteAsync(fileResult.Id);
            }
        }
Beispiel #4
0
        public override async Task <bool> RemoveEntityAsync(ObjectId id)
        {
            var entity = await GetEntityByIdAsync(id);

            if (entity != null && entity.FileId != ObjectId.Empty)
            {
                await _gridFs.DeleteAsync(entity.FileId);
            }
            return(await base.RemoveEntityAsync(id));
        }
Beispiel #5
0
        public async Task <IActionResult> DeleteOneVideoFile(string videoID)
        {
            await gridFS.DeleteAsync(new ObjectId(videoID));//deleting video

            var filter = Builders <Video> .Filter.Eq(x => x.videoFileId, videoID);

            var videoDetail = await video.Find(filter).FirstOrDefaultAsync();

            await video.DeleteOneAsync(filter);

            return(RedirectToAction("Login", "Home", videoDetail.videoUploaderId));
        }
Beispiel #6
0
        public async Task RemoveAsync(string hallId, string id)
        {
            var stand = await GetAsync(hallId, id);

            if (stand?.Photo?.Id != null)
            {
                await _gridFS.DeleteAsync(ObjectId.Parse(stand.Photo.Id));
            }
            var update = Builders <Hall> .Update.PullFilter(hall => hall.Stands,
                                                            stand => stand.Id.Equals(id));

            var result = await _halls
                         .FindOneAndUpdateAsync(hall => hall.Id.Equals(hallId), update);
        }
Beispiel #7
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;
            }
        }
Beispiel #8
0
 /// <summary>
 /// Delete photos by ids from database.
 /// </summary>
 /// <param name="fileIds">ids of the photos to be deleted.</param>
 /// <returns></returns>
 public async Task DeletePhotosByIds(IEnumerable <ObjectId> fileIds)
 {
     foreach (var fileId in fileIds)
     {
         await _gridFs.DeleteAsync(fileId);
     }
 }
Beispiel #9
0
 public void DeleteById(ObjectId id)
 {
     if (id == null)
     {
         throw new ArgumentNullException(nameof(id));
     }
     _gridFsBucket.DeleteAsync(id);
 }
Beispiel #10
0
        // сохранение изображения
        public async Task StoreImage(string id, Stream imageStream, string imageName)
        {
            // если ранее уже была прикреплена картинка, удаляем ее
            await _gridFS.DeleteAsync(new ObjectId(id));

            // сохраняем изображение
            ObjectId imageId = await _gridFS.UploadFromStreamAsync(imageName, imageStream);
        }
Beispiel #11
0
        public async Task RemoveFile(string id, FileTypeEnum fileType)
        {
            var file = await GetFile(id, fileType);

            if (file != null)
            {
                await _gridFS.DeleteAsync(new ObjectId(id));
            }
        }
Beispiel #12
0
        public async Task UpdateAsync(string id, Hall hallIn)
        {
            hallIn.Id = id;
            if (!string.IsNullOrEmpty(hallIn.Photo?.Id))
            {
                await _gridFS.DeleteAsync(ObjectId.Parse(hallIn.Photo.Id));
            }
            if (hallIn.Photo?.Photo != null)
            {
                ObjectId photoId = await _gridFS.UploadFromBytesAsync(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff"), hallIn.Photo.Photo);

                hallIn.Photo.Id    = photoId.ToString();
                hallIn.Photo.Photo = null;
            }
            else
            {
                hallIn.Photo = null;
            }
            await _halls.ReplaceOneAsync(hall => hall.Id.Equals(id), hallIn);
        }
 private async Task DeleteCoreAsync(string id)
 {
     try
     {
         await bucket.DeleteAsync(id);
     }
     catch (GridFSFileNotFoundException)
     {
         return;
     }
 }
Beispiel #14
0
        public async Task <bool> RemoveFileAsync(ObjectId id)
        {
            try
            {
                await gridFSBucket.DeleteAsync(id);

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Beispiel #15
0
        public async Task DeleteAsync(string fileName)
        {
            try
            {
                var name = GetFileName(fileName, nameof(fileName));

                await bucket.DeleteAsync(name);
            }
            catch (GridFSFileNotFoundException)
            {
                return;
            }
        }
Beispiel #16
0
        public async Task <OperationResult> ExecuteAsync(CancellationToken cancellationToken)
        {
            try
            {
                await _bucket.DeleteAsync(_id, cancellationToken);

                return(OperationResult.FromResult(null));
            }
            catch (Exception exception)
            {
                return(OperationResult.FromException(exception));
            }
        }
        public async Task RemoveAsync(string hallId, string standId, string id)
        {
            var exhibit = await GetAsync(hallId, standId, id);

            if (exhibit?.Photos != null)
            {
                foreach (var photo in exhibit.Photos)
                {
                    if (!string.IsNullOrEmpty(photo?.Id))
                    {
                        await _gridFS.DeleteAsync(ObjectId.Parse(photo.Id));
                    }
                }
            }

            var filter = Builders <Hall> .Filter.Eq(hall => hall.Id, hallId);

            var update = Builders <Hall> .Update.PullFilter("Stands.$[].Exhibits",
                                                            Builders <Exhibit> .Filter.Eq(x => x.Id, id));

            var result = await _halls
                         .FindOneAndUpdateAsync(filter, update);
        }
Beispiel #18
0
        public async Task <bool> DeleteFileAsync(ObjectId objectid, string bucketName)
        {
            try
            {
                Bucket = new GridFSBucket(mongoDatabase, new GridFSBucketOptions {
                    BucketName = bucketName
                });
                await Bucket.DeleteAsync(objectid);

                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
Beispiel #19
0
        public async Task StoreImage(string id, Stream imageStream, string imageName)
        {
            var p = await GetProduct(id);

            if (p.HasImage())
            {
                await gridFS.DeleteAsync(new ObjectId(p.imageID));
            }
            var imageId = await gridFS.UploadFromStreamAsync(imageName, imageStream);

            p.imageID = imageId.ToString();
            var filter = Builders <Product> .Filter.Eq("_id", new ObjectId(p.Id));

            var update = Builders <Product> .Update.Set("imageID", p.imageID);

            await products.UpdateOneAsync(filter, update);
        }
Beispiel #20
0
 private async Task DeleteCoreAsync(FileInfo file, CancellationToken ct = default(CancellationToken))
 {
     try
     {
         file.Delete();
         await bucket.DeleteAsync(file.Name, ct);
     }
     catch (FileNotFoundException ex)
     {
         throw new AssetNotFoundException($"Asset {file.Name} not found.", ex);
     }
     catch (GridFSException ex)
     {
         throw new GridFSException(
                   $"Cannot delete file {file.Name} into Mongo GridFS bucket '{bucket.Options.BucketName}'.", ex);
     }
 }
Beispiel #21
0
        public async Task StoreImage(string id, Stream imageStream, string imageName)
        {
            Phone p = await GetPhone(id);

            if (p.HasImage())
            {
                await gridFS.DeleteAsync(new ObjectId(p.ImageId));
            }

            ObjectId imageId = await gridFS.UploadFromStreamAsync(imageName, imageStream);

            p.ImageId = imageId.ToString();
            var filter = Builders <Phone> .Filter.Eq("_id", new ObjectId(p.Id));

            var update = Builders <Phone> .Update.Set("ImageId", p.ImageId);

            await Phones.UpdateOneAsync(filter, update);
        }
Beispiel #22
0
        public async Task <IActionResult> Delete(string id)
        {
            TalentShower ts = await GetUser(id);

            if (ts.hasFile)
            {
                var localVideo = await GetVideoFileId(id);                      //Finding video file ID

                await gridFS.DeleteAsync(new ObjectId(localVideo.videoFileId)); //deleting video

                var filter = Builders <Video> .Filter.Eq(x => x.videoUploaderId, id);

                await video.DeleteOneAsync(filter);
            }
            await talentShower.DeleteOneAsync(new BsonDocument("_id", new ObjectId(id)));

            return(RedirectToAction("Index", "Home"));
        }
        public async Task WriteAsync(LuceneDirectory directory, SnapshotDeletionPolicy snapshotter)
        {
            var directoryInfo = ((FSDirectory)directory).Directory;

            var commit = snapshotter.Snapshot();

            try
            {
                var fileId = directoryInfo.Name;

                try
                {
                    await bucket.DeleteAsync(fileId);
                }
                catch (GridFSFileNotFoundException)
                {
                }

                using (var stream = await bucket.OpenUploadStreamAsync(fileId, fileId))
                {
                    using (var zipArchive = new ZipArchive(stream, ZipArchiveMode.Create, true))
                    {
                        foreach (var fileName in commit.FileNames)
                        {
                            var file = new FileInfo(Path.Combine(directoryInfo.FullName, fileName));

                            using (var fileStream = file.OpenRead())
                            {
                                var entry = zipArchive.CreateEntry(fileStream.Name);

                                using (var entryStream = entry.Open())
                                {
                                    await fileStream.CopyToAsync(entryStream);
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                snapshotter.Release(commit);
            }
        }
        public async Task StoreFile(string id, Stream trackStream, string trackName)
        {
            Track p = await GetTrack(id);

            if (p.HasFile())
            {
                await gridFS.DeleteAsync(new ObjectId(p.FileId));
            }

            ObjectId trackID = await gridFS.UploadFromStreamAsync(trackName, trackStream);

            p.FileId = trackID.ToString();

            var filter = Builders <Track> .Filter.Eq("_id", new ObjectId(p.Id));

            var update = Builders <Track> .Update.Set("FileId", p.FileId);

            await Tracks.UpdateOneAsync(filter, update);
        }
Beispiel #25
0
        //save image
        public async Task StoreImage(string id, Stream imageStream, string imageName)
        {
            Product p = await GetProduct(id);

            if (p.HasImage())
            {
                //if a picture was already attached earlier, delete it
                await gridFS.DeleteAsync(new ObjectId(p.ImageId));
            }
            // save the image
            ObjectId imageId = await gridFS.UploadFromStreamAsync(imageName, imageStream);

            //updating document data
            p.ImageId = imageId.ToString();
            var filter = Builders <Product> .Filter.Eq("_id", new ObjectId(p.Id));

            var update = Builders <Product> .Update.Set("ImageId", p.ImageId);

            await Products.UpdateOneAsync(filter, update);
        }
        // сохранение изображения
        public async Task StoreImage(string email, Stream imageStream, string imageName)
        {
            User c = await GetUserE(email);

            if (c.HasImage())
            {
                // если ранее уже была прикреплена картинка, удаляем ее
                await gridFS.DeleteAsync(new ObjectId(c.ImageId));
            }
            // сохраняем изображение
            ObjectId imageId = await gridFS.UploadFromStreamAsync(imageName, imageStream);

            // обновляем данные по документу
            c.ImageId = imageId.ToString();
            var filter = Builders <User> .Filter.Eq("Email", c.Email);

            var update = Builders <User> .Update.Set("ImageId", c.ImageId);

            await Users.UpdateOneAsync(filter, update);
        }
Beispiel #27
0
        // сохранение изображения
        public async Task StoreImage(string id, Stream imageStream, string imageName)
        {
            ImageWithUrl p = await GetProduct(id);

            if (p.HasImage())
            {
                // если ранее уже была прикреплена картинка, удаляем ее
                await gridFS.DeleteAsync(new ObjectId(p.ImageId));
            }
            // сохраняем изображение
            ObjectId imageId = await gridFS.UploadFromStreamAsync(imageName, imageStream);

            // обновляем данные по документу
            p.ImageId = imageId.ToString();
            var filter = Builders <ImageWithUrl> .Filter.Eq("_id", new ObjectId(p.Id));

            var update = Builders <ImageWithUrl> .Update.Set("ImageId", p.ImageId);

            await imagesWithUrl.UpdateOneAsync(filter, update);
        }
        // сохранение изображения
        public async Task StoreImage(string code, Stream imageStream, string imageName)
        {
            Speciality s = await GetSpecialityCode(code);

            if (s.HasImage())
            {
                await gridFS.DeleteAsync(new ObjectId(s.ImageId));
            }

            // сохраняем изображение
            ObjectId imageId = await gridFS.UploadFromStreamAsync(imageName, imageStream);

            // обновляем данные по документу
            s.ImageId = imageId.ToString();
            var filter = Builders <Speciality> .Filter.Eq("Code", s.Code);

            var update = Builders <Speciality> .Update.Set("ImageId", s.ImageId);

            await Specialities.UpdateOneAsync(filter, update);
        }
Beispiel #29
0
        public async Task <bool> DeleteAsync(Guid id, CancellationToken cancellationToken)
        {
            FilterDefinition <GridFSFileInfo> filter = Builders <GridFSFileInfo> .Filter
                                                       .Eq(x => x.Filename, id.ToString("N"));

            IAsyncCursor <GridFSFileInfo> cursor = await _gridFSBucket
                                                   .FindAsync(filter, options : null, cancellationToken);

            GridFSFileInfo?file = cursor.FirstOrDefault(cancellationToken);

            if (file != null)
            {
                await _gridFSBucket.DeleteAsync(file.Id, cancellationToken);

                Log.Information("Thmbnail {Filename} deleted.", id);
                return(true);
            }

            return(false);
        }
Beispiel #30
0
        public async Task DeleteFileAsync(string taskId)
        {
            try
            {
                var files = await _gridFs.FindAsync(
                    new ExpressionFilterDefinition <GridFSFileInfo>(x => x.Filename == taskId));

                var file = await files.FirstOrDefaultAsync();

                if (file != null)
                {
                    await _gridFs.DeleteAsync(file.Id);
                }
            }
            catch (Exception e)
            {
                _logger.LogError("Error occured while uploading {@TaskId} in {@AgentName} with {@Exception}",
                                 taskId, nameof(FileWorkerAgentImpl), e.ToString());
            }
        }