public async Task <GridFSFileInfo> DownloadFileAsync(ObjectId id, GridFSDownloadOptions options = null)
        {
            var gridFsBucket = new GridFSBucket(this.database);

            using (GridFSDownloadStream sourceStream = await gridFsBucket.OpenDownloadStreamAsync(id))
            {
                long filezise = sourceStream.Length;
                //缓冲区大小
                int    maxlenth = 4096;
                byte[] buffer   = new byte[maxlenth];
                using (FileStream destinationStream = File.Open(_confg + "/" + sourceStream.FileInfo.Filename, FileMode.Create))
                {
                    while (filezise > 0)
                    {
                        if (filezise < maxlenth)
                        {
                            maxlenth = (int)filezise;
                        }
                        await sourceStream.ReadAsync(buffer, 0, maxlenth);

                        await destinationStream.WriteAsync(buffer, 0, maxlenth);

                        filezise -= maxlenth;
                    }
                }
                return(sourceStream.FileInfo);
            }
        }
Example #2
0
        public async Task <string> DownloadFileAsync(GridFSFileInfo file)
        {
            var    gridFsBucket = new GridFSBucket(GetDatabase());
            string filename     = file.Metadata["fileName"].AsString;
            string targetDir    = Path.Combine(ProgramUI.GetWorkspaceDirectory(), MongoConfig.MongoConfigName);

            if (!Directory.Exists(targetDir))
            {
                Directory.CreateDirectory(targetDir);
            }

            string extension = ".bin";

            if (file.ContentType.Equals("plain/text", StringComparison.CurrentCultureIgnoreCase))
            {
                extension = ".txt";
            }
            string targetFilename = Path.Combine(targetDir, filename + "_" + file.Id.ToString() + extension);

            Logger.Log($"Saving file to {targetFilename}");
            // using (GridFSDownloadStream<ObjectId> sourceStream = await gridFsBucket.OpenDownloadStreamByNameAsync(filename))
            using (GridFSDownloadStream sourceStream = await gridFsBucket.OpenDownloadStreamAsync(file.Id))
            {
                using (FileStream destinationStream = File.Open(targetFilename, FileMode.Create))
                {
                    await sourceStream.CopyToAsync(destinationStream);
                }
            }
            return(targetFilename);
        }
        public async Task <IFileResponse> GetPostFileAsync(string fileId)
        {
            try
            {
                var bucket = new GridFSBucket(_mongoDbContext.Database);
                var file   = new FileView();
                using (var stream = await bucket.OpenDownloadStreamAsync(new ObjectId(fileId)))
                {
                    if (stream == null)
                    {
                        return(new FileResponse(ClientMessageConstant.FileNotFound, HttpStatusCode.NotFound));
                    }
                    file.FileBytes = await bucket.DownloadAsBytesAsync(new ObjectId(fileId));

                    file.Name     = stream.FileInfo.Filename;
                    file.MimeType = stream.FileInfo.ContentType;
                }

                return(new FileResponse(file));
            }
            catch (Exception e)
            {
                return(new FileResponse(e));
            }
        }
Example #4
0
        public async Task <IList <FileDataModel> > DownloadsAsync(string siteId = null, string parentId = null, bool isGetStream = true, bool isGetUrl = false)
        {
            var builder = Builders <GridFSFileInfo> .Filter;
            var filter  = builder.Empty;

            if (!string.IsNullOrEmpty(siteId))
            {
                filter = builder.Eq("metadata.id", siteId);
                if (!string.IsNullOrEmpty(parentId))
                {
                    filter = filter & builder.Eq("metadata.parentId", parentId);
                }
            }
            var gridFs = new GridFSBucket(_database);
            var files  = await gridFs.Find(filter).ToListAsync();

            var results = new List <FileDataModel>();

            if (files == null)
            {
                return(results);
            }
            foreach (var mongoGridFsFileInfo in files)
            {
                var fileDataModel = MapFileDataModel(mongoGridFsFileInfo);
                if (isGetStream)
                {
                    fileDataModel.FileData.Stream = await gridFs.OpenDownloadStreamAsync(mongoGridFsFileInfo.Id);
                }
                results.Add(fileDataModel);
            }

            return(results);
        }
Example #5
0
        public override async Task <FileResult> GetAsync(string id)
        {
            ObjectId oid = new ObjectId(id);

            var stream = await _bucket.OpenDownloadStreamAsync(oid);

            return(new FileResult(stream, (string)stream.FileInfo.Metadata[ContentType]));
        }
Example #6
0
        public async Task <(Stream, FileDetails)> DownloadFileAsync(string id)
        {
            var collection = fileInfoDB.GetCollection <FileDetails>(fileInfoDbName);
            var results    = await collection.FindAsync(fileInfo => fileInfo.Id.Equals(id));

            var fileDetails = await results.FirstOrDefaultAsync();

            return(await fsBucket.OpenDownloadStreamAsync(ObjectId.Parse(id)), fileDetails);
        }
        public async Task <GridFSFileInfo> GetFileInfoAsync(string id)
        {
            var gridFsBucket = new GridFSBucket(this.database);

            using (GridFSDownloadStream sourceStream = await gridFsBucket.OpenDownloadStreamAsync(new ObjectId(id)))
            {
                return(sourceStream.FileInfo);
            }
        }
        public async Task <IFileResponse> GetMessagingImageFileAsync(string roomId)
        {
            try
            {
                var file = new FileView();
                var room = await _mongoDbContext.Rooms.Find(k => k.ID == new ObjectId(roomId)).FirstOrDefaultAsync();

                if (room == null)
                {
                    return(new FileResponse(ClientMessageConstant.PostNotFound, HttpStatusCode.NotFound));
                }

                //if (room.Messages. == (int)PostType.Image)
                //{
                var fileId = room.Messages.FirstOrDefault().ImagesIDs.ToString();

                if (fileId == null)
                {
                    return(new FileResponse(file));
                }

                var bucket = new GridFSBucket(_mongoDbContext.Database);

                using (var stream = await bucket.OpenDownloadStreamAsync(new ObjectId(fileId)))
                {
                    if (stream == null)
                    {
                        return(new FileResponse(ClientMessageConstant.FileNotFound, HttpStatusCode.NotFound));
                    }
                    file.FileBytes = await bucket.DownloadAsBytesAsync(new ObjectId(fileId));

                    file.Name     = stream.FileInfo.Filename;
                    file.MimeType = stream.FileInfo.ContentType;
                }
                //}
                //else
                //{
                //  return new FileResponse(ClientMessageConstant.ImagePostNotExist, HttpStatusCode.NotFound);
                //}


                return(new FileResponse(file));
            }
            catch (Exception e)
            {
                return(new FileResponse(e));
            }
        }
Example #9
0
        public async Task <dynamic> Get([FromRoute] string id)
        {
            Request.Headers.TryGetValue("If-None-Match", out StringValues clientEtag);
            string clientEtagStr = clientEtag.Count > 0 ? clientEtag.ToString() : null;

            try
            {
                var gridFsBucket   = new GridFSBucket <ObjectId>(MongoWrapper.Database);
                var downloadStream = await gridFsBucket.OpenDownloadStreamAsync
                                     (
                    new ObjectId(id),
                    new GridFSDownloadOptions
                {
                    Seekable = true,
                    CheckMD5 = false
                }
                                     );

                if (clientEtagStr != null)
                {
                    if (downloadStream.FileInfo.MD5.Equals(clientEtagStr.Trim('"'), StringComparison.Ordinal))
                    {
                        Response.StatusCode = (int)HttpStatusCode.NotModified;
                        return(new EmptyResult());
                    }
                }

                var    fileMetadata = BsonSerializer.Deserialize <FileMetadata>(downloadStream.FileInfo.Metadata);
                string contentType  = string.IsNullOrWhiteSpace(fileMetadata.ContentType)
                                     ? "application/octet-stream" : fileMetadata.ContentType;
                Response.GetTypedHeaders().CacheControl = CacheControlHeaderValue.Parse(CacheControlHeaderValue.NoCacheString);
                return(new FileStreamResult(downloadStream, contentType)
                {
                    EnableRangeProcessing = true,
                    EntityTag = new EntityTagHeaderValue("\"" + downloadStream.FileInfo.MD5 + "\"")
                });
            }
            catch (GridFSFileNotFoundException)
            {
                return(new ResponseBody
                {
                    Code = ResponseCode.NotFound,
                    Message = "Arquivo não foi encontrado!",
                    Success = false
                });
            }
        }
        public async Task <IFileResponse> GetPostImageAsync(string postId)
        {
            try
            {
                var file = new FileView();
                var post = await _mongoDbContext.Posts.Find(k => k.ID == new ObjectId(postId)).FirstOrDefaultAsync();

                if (post == null)
                {
                    return(new FileResponse(ClientMessageConstant.PostNotFound, HttpStatusCode.NotFound));
                }

                if (post.TypeID == (int)PostType.Image)
                {
                    var fileId = post.ImageIDs.FirstOrDefault();

                    if (fileId == null)
                    {
                        return(new FileResponse(file));
                    }

                    var bucket = new GridFSBucket(_mongoDbContext.Database);

                    using (var stream = await bucket.OpenDownloadStreamAsync(new ObjectId(fileId)))
                    {
                        if (stream == null)
                        {
                            return(new FileResponse(ClientMessageConstant.FileNotFound, HttpStatusCode.NotFound));
                        }
                        file.FileBytes = await bucket.DownloadAsBytesAsync(new ObjectId(fileId));

                        file.Name     = stream.FileInfo.Filename;
                        file.MimeType = stream.FileInfo.ContentType;
                    }
                }
                else
                {
                    return(new FileResponse(ClientMessageConstant.ImagePostNotExist, HttpStatusCode.NotFound));
                }
                return(new FileResponse(file));
            }
            catch (Exception e)
            {
                return(new FileResponse(e));
            }
        }
Example #11
0
        public async Task <IActionResult> IndexAsync(string id)
        {
            var imgId = id.ToObjectId();
            var media = imgId == default ? await _media.GetMedia(id) : await _media.GetMedia(imgId);

            if (media == null)
            {
                return(NotFound());
            }
            var file = await _gridFS.OpenDownloadStreamAsync(media.MediaId);

            return(media.MediaType switch
            {
                MediaType.Image => File(file, media.Ext == ".gif" ? "image/gif" : "image/png"),
                MediaType.Text => File(file, "text"),
                MediaType.Code => File(file, "text"),
                MediaType.Video => File(file, "video"),
                _ => File(file, "application/octet-stream")
            });
Example #12
0
        /*public IList<FileDataModel> GetFiles(string siteId, string parentId)
         * {
         *  return Downloads(siteId, parentId, false);
         *  var builder = Builders<GridFSFileInfo>.Filter;
         *  var filter = builder.Eq("metadata.id", siteId) & builder.Eq("metadata.parentId", parentId);
         *
         *  var gridFs = new GridFSBucket(_database);
         *  var files = gridFs.Find(filter).ToList();
         *
         *  var results = new List<FileDataModel>();
         *  if (files == null)
         *  {
         *      return results;
         *  }
         *
         *  foreach (var mongoGridFsFileInfo in files)
         *  {
         *      var fileDataModel = MapFileDataModel(mongoGridFsFileInfo);
         *      results.Add(fileDataModel);
         *  }
         *
         *  return results;
         * }*/

        public async Task <FileDataModel> DownloadAsync(string siteId, string parentId, string propertyName, string module, bool isGetStream = true, bool isGetUrl = false)
        {
            var builder = Builders <GridFSFileInfo> .Filter;
            var filter  = builder.Eq("metadata.id", siteId) & builder.Eq("metadata.parentId", parentId) & builder.Eq("metadata.propertyName", propertyName) & builder.Eq("metadata.module", module);

            var gridFs = new GridFSBucket(_database);
            var file   = await gridFs.Find(filter).FirstOrDefaultAsync();

            if (file == null)
            {
                return(null);
            }

            var fileDataModel = MapFileDataModel(file);

            fileDataModel.FileData.Stream = await gridFs.OpenDownloadStreamAsync(file.Id);

            return(fileDataModel);
        }
        private async Task <int> NormalizeAudio(FileReference fileReference, CreateSongRequest requestBody, string authorName)
        {
            var oldId = fileReference._id;
            var newId = ObjectId.GenerateNewId();

            var gridFsBucket   = new GridFSBucket <ObjectId>(MongoWrapper.Database);
            var downloadStream = await gridFsBucket.OpenDownloadStreamAsync(oldId);

            (Stream newAudio, int seconds) = await AudioHandlerService.ProcessAudio
                                             (
                downloadStream,
                requestBody.Autoral?null : new int?(15),
                requestBody.Nome,
                authorName,
                fileReference.FileInfo.FileMetadata.ContentType
                                             );

            fileReference._id = newId;
            if (fileReference.FileInfo == null)
            {
                fileReference.FileInfo = new Models.FileInfo();
            }
            fileReference.FileInfo.FileMetadata.ContentType = "audio/mpeg";
            fileReference.FileInfo.FileMetadata.FileType    = FileType.Audio;

            var uploadStream = await gridFsBucket.OpenUploadStreamAsync
                               (
                newId,
                newId.ToString(),
                new GridFSUploadOptions
            {
                Metadata = fileReference.FileInfo.FileMetadata.ToBsonDocument(),
            }
                               );

            await newAudio.CopyToAsync(uploadStream);

            await uploadStream.CloseAsync();

            var deleteOldTask = gridFsBucket.DeleteAsync(oldId);

            return(seconds);
        }
Example #14
0
 /// <summary>
 /// 下载文件
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public async Task <Stream> GetFileStreamAsync(string id)
 {
     return(await _bucket.OpenDownloadStreamAsync(new ObjectId(id)));
 }
Example #15
0
        private static Task StartRadioFeeder(RadioCastServer radioCastServer, MongoWrapper mongoWrapper)
        {
            var userCollection = mongoWrapper.Database.GetCollection <Models.Musician>(nameof(Models.User));

            var songSortBuilder = new SortDefinitionBuilder <ProjectedMusicianSong>();
            var songSort        = songSortBuilder
                                  .Descending(nameof(ProjectedMusicianSong.Score));

            var userFilterBuilder = new FilterDefinitionBuilder <Models.Musician>();
            var userFilter        = userFilterBuilder.And
                                    (
                GeneralUtils.NotDeactivated(userFilterBuilder),
                userFilterBuilder.AnyEq("_t", nameof(Models.Musician))
                                    );

            var songFilterBuilder = new FilterDefinitionBuilder <ProjectedMusicianSong>();
            var songFilter        = songFilterBuilder.And
                                    (
                GeneralUtils.NotDeactivated(songFilterBuilder, s => s.Song),
                songFilterBuilder.Eq(s => s.Song.RadioAuthorized, true),
                songFilterBuilder.Eq(s => s.Song.Original, true)
                                    );

            var projectionBuilder = new ProjectionDefinitionBuilder <Models.Musician>();
            var projection        = projectionBuilder.Include(m => m.FullName).Include(m => m.Songs);

            var fsBucket = new GridFSBucket <ObjectId>(mongoWrapper.Database);

            var trackHistory = new List <(IAudioSource, ProjectedMusicianSong)>();

            var onTrackChangedTE = new ManualResetEvent(true);

            radioCastServer.OnTrackChanged += async(s, e) =>
            {
                List <(IAudioSource, ProjectedMusicianSong)> myTrackHistory;
                lock (trackHistory)
                {
                    myTrackHistory = trackHistory.ToList();
                }

                RadioInfoController.CurrentSong = myTrackHistory.Where(th => th.Item1.Equals(e.NewTrack)).Select(th => th.Item2).LastOrDefault();
                LOGGER.Info("Now playing: {}", JsonConvert.SerializeObject(RadioInfoController.CurrentSong));

                onTrackChangedTE.Set();

                if (e.OldTrack == null)
                {
                    return;
                }

                var oldTrack      = myTrackHistory.Where(th => th.Item1.Equals(e.OldTrack)).Select(th => th.Item2).LastOrDefault();
                var oldMusicianId = oldTrack._id;
                var oldTrackId    = oldTrack.Song._id;

                var musSongFilterBuilder = new FilterDefinitionBuilder <Models.Musician>();

                var musSongFilter = musSongFilterBuilder.And
                                    (
                    musSongFilterBuilder.Eq(m => m._id, oldMusicianId),
                    musSongFilterBuilder.ElemMatch(m => m.Songs, sg => sg._id == oldTrackId)
                                    );
                var musSongUpdate = new UpdateDefinitionBuilder <Models.Musician>()
                                    .Inc($"{nameof(Models.Musician.Songs)}.$.{nameof(Song.TimesPlayedRadio)}", 1);

                await userCollection.UpdateOneAsync(musSongFilter, musSongUpdate);

                // Remove oldest, only keep 5 in history
                if (myTrackHistory.Count > 5)
                {
                    lock (trackHistory)
                    {
                        trackHistory.RemoveAt(0);
                    }
                }
            };

            return(Task.Run(async() =>
            {
                while (true)
                {
                    List <(IAudioSource, ProjectedMusicianSong)> myTrackHistory;
                    lock (trackHistory)
                    {
                        myTrackHistory = trackHistory.ToList();
                    }

                    var lookupStageRandomArr = $@"
                    {{
                        $lookup:
                        {{
                            from: ""randomNumbers"",
                            pipeline:
                            [
                                {{ $sample: {{ size: 2 }} }}
                            ],
                            as: ""RandomArr""
                        }}
                    }}
                    ";

                    // OK Vezes totais que a música foi tocada * 0.5
                    // OK Vezes totais que a música foi tocada na rádio * -1
                    // OK Se música está presente na lista das últimas 5 tocadas, -100
                    // OK Se o autor da música está presente na lista das últimas 5 tocadas, -50
                    // OK Pontuação aleatória para cada música, entre - 10 e + 10
                    // OK Número de dias desde o cadastramento da música * -1
                    // OK   Há uma chance de 5% de multiplicar a pontuação resultante por -1 (efeito nostalgia)

                    var scoreStage = $@"
                    {{
                        $addFields:
                        {{
                            ""Score"":
                            {{
                                $add:
                                [
                                    {{
                                        $multiply: [ ""$Song.timesPlayed"", 0.5 ]
                                    }},
                                    {{
                                        $multiply: [ ""$Song.timesPlayedRadio"", -1 ]
                                    }},
                                    {{
                                        $cond:
                                        {{
                                            if:
                                            {{
                                                $in: [ ""$_id"", [ {myTrackHistory.Select(th => $"ObjectId(\"{th.Item2._id.ToString()}\")").DefaultIfEmpty("").Aggregate((s1, s2) => $"{s1.TrimEnd(',')},{s2.TrimEnd(',')},").TrimEnd(',')} ] ]
                                            }},
                                            then: -50,
                                            else: 0
                                        }}
                                    }},
                                    {{
                                        $cond:
                                        {{
                                            if:
                                            {{
                                                $in: [ ""$Song._id"", [ {myTrackHistory.Select(th => $"ObjectId(\"{th.Item2.Song._id.ToString()}\")").DefaultIfEmpty("").Aggregate((s1, s2) => $"{s1.TrimEnd(',')},{s2.TrimEnd(',')},").TrimEnd(',')} ] ]
                                            }},
                                            then: -100,
                                            else: 0
                                        }}
                                    }},
                                    {{
                                        $add:
                                        [ 
                                            {{
                                                $multiply:
                                                [
                                                    {{ $toDecimal: {{ $arrayElemAt: [""$RandomArr.decimal"", 0] }} }},
                                                    21
                                                ]
                                            }}, 
                                            -10
                                        ]
                                    }},
                                    {{
                                        $multiply:
                                        [
                                            {{
                                                $divide:
                                                [
                                                    {{
                                                        $subtract:
                                                        [
                                                            {{ $toDate: ""{DateTime.UtcNow.ToString("o")}"" }},
                                                            {{ $toDate: ""$Song._id"" }}
                                                        ]
                                                    }},
                                                    NumberLong(""86400000"")
                                                ]
                                            }},
                                            {{
                                                $cond:
                                                {{
                                                    if:
                                                    {{
                                                        $gt:
                                                        [
                                                            {{ $toDecimal: {{ $arrayElemAt: [""$RandomArr.decimal"", 1] }} }},
                                                            NumberDecimal(""0.1"")
                                                        ]
                                                    }},
                                                    then: -1,
                                                    else: 1
                                                }}
                                            }}
                                        ]
                                    }}
                                ]
                            }}
                        }}
                    }}";

                    LOGGER.Info("Score stage generated MongoDB query: {}", scoreStage);

                    var pipeline = PipelineDefinitionBuilder
                                   .For <Models.Musician>()
                                   .Match(userFilter)
                                   .Unwind(m => m.Songs, new AggregateUnwindOptions <Models.Musician>
                    {
                        PreserveNullAndEmptyArrays = false,
                        IncludeArrayIndex = null,
                    })
                                   .Project(m => new ProjectedMusicianSong
                    {
                        _id = m._id,
                        Song = (Song)m.Songs,
                        Score = 1,
                    })
                                   .Match(songFilter)
                                   .AppendStage <Musician, ProjectedMusicianSong, ProjectedMusicianSong>(lookupStageRandomArr)
                                   .AppendStage <Musician, ProjectedMusicianSong, ProjectedMusicianSong>(scoreStage)
                                   .Sort(songSort)
                                   .Limit(1);

                    if (LOGGER.IsDebugEnabled)
                    {
                        LOGGER.Debug("Pipeline generated MongoDB query for song: {}", pipeline.ToString());
                    }

                    var findTask = userCollection.AggregateAsync(pipeline, new AggregateOptions
                    {
                        AllowDiskUse = true,
                        BatchSize = 1,
                        UseCursor = true,
                        Comment = "Radio Aggregate Query",
                        TranslationOptions = new ExpressionTranslationOptions
                        {
                            StringTranslationMode = AggregateStringTranslationMode.CodePoints
                        }
                    });

                    var findResult = await findTask;
                    var firstSong = findResult.SingleOrDefault();
                    // If no songs, wait a minute before checking again
                    // Mostly not to strain the CPU on development environments
                    if (firstSong?.Song == null)
                    {
                        Thread.Sleep(TimeSpan.FromMinutes(1));
                        continue;
                    }
                    LOGGER.Info("Next selected song: {}", JsonConvert.SerializeObject(firstSong));
                    var audioRef = firstSong.Song.AudioReference;
                    var gridId = audioRef._id;
                    var fileStreamTask = fsBucket.OpenDownloadStreamAsync(gridId, new GridFSDownloadOptions
                    {
                        Seekable = true,
                        CheckMD5 = false,
                    });
                    var audioSource = new Mp3FileAudioSource(await fileStreamTask, firstSong.Song.Name);
                    // Wait for the radio to need more songs before we add the track we have on our hands
                    while (radioCastServer.TrackCount > 1)
                    {
                        onTrackChangedTE.WaitOne();
                        onTrackChangedTE.Reset();
                    }
                    lock (trackHistory)
                    {
                        trackHistory.Add((audioSource, firstSong));
                    }
                    radioCastServer.AddTrack(audioSource);
                }
            }));
        }
Example #16
0
 public async Task <Stream> DownloadFileAsync(string id)
 {
     return(await fsBucket.OpenDownloadStreamAsync(ObjectId.Parse(id)));
 }