Ejemplo n.º 1
0
        private static void UploadFileAndSetMetadata(DateTime absoluteExpiration, MemoryStream imageStream, LiteStorage fileStorage, string id)
        {
            imageStream.Position = 0;
            var fileInfo = fileStorage.Upload(id, null, imageStream);

            fileInfo.Metadata.Add(new KeyValuePair <string, BsonValue>("Expires", absoluteExpiration));
            imageStream.Position = 0;
        }
Ejemplo n.º 2
0
        public async Task <Stream> GetImageAsync(string path)
        {
            try
            {
                if (path.StartsWith("/"))
                {
                    return(await ImageClient.GetStreamAsync("original" + path));
                }
                else if (!path.StartsWith("w"))
                {
                    return(await YouTubeClient.GetStreamAsync(String.Format("{0}/0.jpg", path)));
                }

                string       id       = path.ComputeHash();
                LiteFileInfo fileInfo = FileStorage.FindById(id);
                if (fileInfo == null)
                {
                    try
                    {
                        using (Stream stream = await ImageClient.GetStreamAsync(path))
                        {
                            if (stream != null)
                            {
                                using (MemoryStream memoryStream = new MemoryStream())
                                {
                                    stream.CopyTo(memoryStream);
                                    if (memoryStream.Length > 0)
                                    {
                                        memoryStream.Position = 0;
                                        fileInfo = FileStorage.Upload(id, path, memoryStream);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        return(null);
                    }
                }

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

                MemoryStream cachedStream = new MemoryStream();
                fileInfo.CopyTo(cachedStream);
                cachedStream.Position = 0;

                return(cachedStream);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Ejemplo n.º 3
0
        private static void UploadFileAndSetMetadata(DateTime absoluteExpiration, MemoryStream imageStream, LiteStorage fileStorage, string id)
        {
            imageStream.Position = 0;
            var fileInfo = fileStorage.Upload(id, null, imageStream);

            fileStorage.SetMetadata(
                fileInfo.Id,
                new BsonDocument(new Dictionary <string, BsonValue> {
                { "Expires", absoluteExpiration }
            }));

            imageStream.Position = 0;
        }
Ejemplo n.º 4
0
        public void Execute(LiteEngine engine, StringScanner s, Display display, InputCommand input, Env env)
        {
            var fs = new LiteStorage(engine);
            var id = this.ReadId(s);

            var filename = s.Scan(@"\s*.*").Trim();

            if (!File.Exists(filename)) throw new IOException("File " + filename + " not found");

            var file = fs.Upload(id, filename);

            display.WriteResult(file.AsDocument);
        }
Ejemplo n.º 5
0
        public IEnumerable <BsonValue> Execute(StringScanner s, LiteEngine engine)
        {
            var fs = new LiteStorage(engine);
            var id = this.ReadId(s);

            var filename = s.Scan(@"\s*.*").Trim();

            if (!File.Exists(filename))
            {
                throw new IOException("File " + filename + " not found");
            }

            var file = fs.Upload(id, filename);

            yield return(file.AsDocument);
        }
Ejemplo n.º 6
0
        public void Execute(LiteEngine engine, StringScanner s, Display display, InputCommand input, Env env)
        {
            var fs = new LiteStorage(engine);
            var id = this.ReadId(s);

            var filename = s.Scan(@"\s*.*").Trim();

            if (!File.Exists(filename))
            {
                throw new IOException("File " + filename + " not found");
            }

            var file = fs.Upload(id, filename);

            display.WriteResult(file.AsDocument);
        }
Ejemplo n.º 7
0
 private bool SetCacheImage(string id, string imageUrl, IInputStream stream)
 {
     try
     {
         id = $"$/{id}";
         var file = _fileStorage.Upload(id, imageUrl, stream.AsStreamForRead());
         _fileStorage.SetMetadata(id, new BsonDocument(new Dictionary <string, BsonValue>()
         {
             { "updateAt", DateTime.Now }
         }));
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Ejemplo n.º 8
0
        public async Task <string> AddSongFromYoutube(string videoId)
        {
            var songId   = $"youtube/{videoId}";
            var songData = await GetSong(songId);

            if (songData != null)
            {
                return(songData.Id);
            }

            ManualResetEvent waitEvent;
            bool             shouldWait = false;

            lock (currentDownloadsLock)
            {
                // check whether another task is already downloading the same song
                if (currentDownloads.ContainsKey(songId))
                {
                    waitEvent  = currentDownloads[songId];
                    shouldWait = true;
                }
                else
                {
                    // we're the first task, create an event for other tasks to wait on
                    waitEvent = new ManualResetEvent(false);
                    currentDownloads.Add(songId, waitEvent);
                }
            }

            if (shouldWait)
            {
                // this task has to wait
                await Task.Run(() => waitEvent.WaitOne());

                // done waiting, just return whatever the first task downloaded (might be null)
                songData = await GetSong(songId);

                return(songData?.Id);
            }

            try
            {
                logger.LogDebug($"Downloading new song from Youtube (Video ID: {videoId})");
                var videoInfo = await downloader.GetVideoInfo(videoId);

                if (videoInfo.Metadata.Duration > config.Music.MaximumSongLengthTimeSpan)
                {
                    throw new VideoTooLongException(config.Music.MaximumSongLengthTimeSpan);
                }
                if (videoInfo.Metadata.Duration == TimeSpan.Zero)
                {
                    throw new VideoIsLivestreamException();
                }
                var stream = await videoInfo.GetOggAudioStream();

                if (stream == null)
                {
                    logger.LogError($"Downloading Youtube video {videoId} failed");
                    return(null);
                }
                var fileId = $"$/music/oggopus/youtube/{videoId}";
                var info   = await Task.Run(() => fileStorage.Upload(fileId, videoId, stream));

                songData = new SongData()
                {
                    Id         = songId,
                    FileId     = fileId,
                    Metadata   = videoInfo.Metadata,
                    LastAccess = DateTime.Now
                };
                await Task.Run(() => collection.Upsert(songData));

                return(songId);
            }
            finally
            {
                // signal other waiting tasks and delete the wait entry
                lock (currentDownloadsLock)
                {
                    waitEvent.Set();
                    currentDownloads.Remove(songId);
                }
            }
        }
Ejemplo n.º 9
0
 public TFile Add(TFile file)
 {
     _fileStorage.Upload(file.Id, nameof(file), new MemoryStream(file.Content));
     return(file);
 }
Ejemplo n.º 10
0
 public void FileUpload(string key, string filePath)
 {
     files.Upload(key, filePath);
 }
Ejemplo n.º 11
0
        public string FileUpload(string id, string fname, Stream filestream)
        {
            var res = _storage.Upload(id, fname, filestream);

            return(res.Id);
        }