public async ValueTask <ThumbnailCache?> FindOneAsync(NestedPath filePath, int width, int height, ThumbnailResizeType resizeType, ThumbnailFormatType formatType)
        {
            await Task.Delay(1).ConfigureAwait(false);

            using (await _asyncLock.LockAsync())
            {
                var id = new ThumbnailCacheIdEntity()
                {
                    FilePath            = NestedPathEntity.Import(filePath),
                    ThumbnailWidth      = width,
                    ThumbnailHeight     = height,
                    ThumbnailResizeType = resizeType,
                    ThumbnailFormatType = formatType,
                };
                var storage = this.GetStorage();

                var liteFileInfo = storage.FindById(id);
                if (liteFileInfo is null)
                {
                    return(null);
                }

                using (var inStream = liteFileInfo.OpenRead())
                {
                    return(RocketMessage.FromStream <ThumbnailCache>(inStream));
                }
            }
        }
        private async ValueTask <IMemoryOwner <byte>[]> GetMovieImagesAsync(NestedPath filePath, TimeSpan minInterval, int maxImageCount, int width, int height, ThumbnailResizeType resizeType, ThumbnailFormatType formatType, CancellationToken cancellationToken = default)
        {
            var resultMap = new ConcurrentDictionary <int, IMemoryOwner <byte> >();

            using var extractedFileOwner = await _fileSystem.ExtractFileAsync(filePath, cancellationToken);

            var duration = await GetMovieDurationAsync(extractedFileOwner.Path, cancellationToken).ConfigureAwait(false);

            int intervalSeconds = (int)Math.Max(minInterval.TotalSeconds, duration.TotalSeconds / maxImageCount);
            int imageCount      = (int)(duration.TotalSeconds / intervalSeconds);

            await Enumerable.Range(1, imageCount)
            .Select(x => x * intervalSeconds)
            .Where(seekSec => (duration.TotalSeconds - seekSec) > 1)     // 残り1秒以下の場合は除外
            .ForEachAsync(
                async seekSec =>
            {
                var ret = await this.GetMovieImagesAsync(extractedFileOwner.Path, seekSec, width, height, resizeType, formatType, cancellationToken);
                resultMap.TryAdd(ret.SeekSec, ret.MemoryOwner);
            }, (int)_concurrency, cancellationToken).ConfigureAwait(false);

            cancellationToken.ThrowIfCancellationRequested();

            if (!resultMap.IsEmpty)
            {
                var tempList = resultMap.ToList();
                tempList.Sort((x, y) => x.Key.CompareTo(y.Key));

                return(tempList.Select(n => n.Value).ToArray());
            }

            var ret = await this.GetMovieImageAsync(extractedFileOwner.Path, width, height, resizeType, formatType, cancellationToken);

            return(new[] { ret });
        }
Exemple #3
0
 public static NestedPathEntity Import(NestedPath value)
 {
     return(new NestedPathEntity()
     {
         Values = value.Values.ToArray()
     });
 }
Exemple #4
0
        public DirectoryModel(NestedPath path, IFileSystem fileSystem)
        {
            this.Path   = path;
            _fileSystem = fileSystem;

            if (path == NestedPath.Empty)
            {
                return;
            }

            this.Children = new[] { new DirectoryModel(NestedPath.Empty, _fileSystem) };
        }
Exemple #5
0
    public async ValueTask <ThumbnailGeneratorGetThumbnailResult> GetThumbnailAsync(NestedPath filePath, ThumbnailGeneratorGetThumbnailOptions options, bool cacheOnly = false, CancellationToken cancellationToken = default)
    {
        await Task.Delay(1, cancellationToken).ConfigureAwait(false);

        if (!await _fileSystem.ExistsFileAsync(filePath, cancellationToken))
        {
            return(new ThumbnailGeneratorGetThumbnailResult(ThumbnailGeneratorGetThumbnailResultStatus.Failed));
        }

        ThumbnailGeneratorGetThumbnailResult result = default;

        // Cache
        result = await this.GetThumbnailFromCacheAsync(filePath, options, cancellationToken).ConfigureAwait(false);

        if (result.Status == ThumbnailGeneratorGetThumbnailResultStatus.Succeeded)
        {
            return(result);
        }

        if (cacheOnly)
        {
            return(new ThumbnailGeneratorGetThumbnailResult(ThumbnailGeneratorGetThumbnailResultStatus.Failed));
        }

        // Picture
        result = await this.GetPictureThumbnailAsync(filePath, options, cancellationToken).ConfigureAwait(false);

        if (result.Status == ThumbnailGeneratorGetThumbnailResultStatus.Succeeded)
        {
            return(result);
        }

        // Movie
        result = await this.GetMovieThumbnailAsync(filePath, options, cancellationToken).ConfigureAwait(false);

        if (result.Status == ThumbnailGeneratorGetThumbnailResultStatus.Succeeded)
        {
            return(result);
        }

        return(new ThumbnailGeneratorGetThumbnailResult(ThumbnailGeneratorGetThumbnailResultStatus.Failed));
    }
Exemple #6
0
    private async ValueTask <ThumbnailGeneratorGetThumbnailResult> GetMovieThumbnailAsync(NestedPath filePath, ThumbnailGeneratorGetThumbnailOptions options, CancellationToken cancellationToken = default)
    {
        if (!_movieTypeExtensionList.Contains(filePath.GetExtension().ToLower()))
        {
            return(new ThumbnailGeneratorGetThumbnailResult(ThumbnailGeneratorGetThumbnailResultStatus.Failed));
        }

        try
        {
            var fileLength = await _fileSystem.GetFileSizeAsync(filePath, cancellationToken);

            var fileLastWriteTime = await _fileSystem.GetFileLastWriteTimeAsync(filePath, cancellationToken);

            var images = await this.GetMovieImagesAsync(filePath, options.MinInterval, options.MaxImageCount, options.Width, options.Height, options.ResizeType, options.FormatType, cancellationToken).ConfigureAwait(false);

            var fileMeta      = new FileMeta(filePath, (ulong)fileLength, Timestamp.FromDateTime(fileLastWriteTime));
            var thumbnailMeta = new ThumbnailMeta(options.ResizeType, options.FormatType, (uint)options.Width, (uint)options.Height);
            var contents      = images.Select(n => new ThumbnailContent(n)).ToArray();
            var cache         = new ThumbnailCache(fileMeta, thumbnailMeta, contents);

            await _thumbnailGeneratorRepository.ThumbnailCaches.InsertAsync(cache);

            return(new ThumbnailGeneratorGetThumbnailResult(ThumbnailGeneratorGetThumbnailResultStatus.Succeeded, cache.Contents));
        }
        catch (NotSupportedException e)
        {
            _logger.Warn(e);
        }
        catch (OperationCanceledException e)
        {
            _logger.Debug(e);
        }
        catch (Exception e)
        {
            _logger.Error(e);
            throw;
        }

        return(new ThumbnailGeneratorGetThumbnailResult(ThumbnailGeneratorGetThumbnailResultStatus.Failed));
    }
Exemple #7
0
    private async ValueTask <ThumbnailGeneratorGetThumbnailResult> GetPictureThumbnailAsync(NestedPath filePath, ThumbnailGeneratorGetThumbnailOptions options, CancellationToken cancellationToken = default)
    {
        var ext = filePath.GetExtension().ToLower();

        if (!_pictureTypeExtensionList.Contains(ext))
        {
            return(new ThumbnailGeneratorGetThumbnailResult(ThumbnailGeneratorGetThumbnailResultStatus.Failed));
        }

        try
        {
            var fileLength = await _fileSystem.GetFileSizeAsync(filePath, cancellationToken);

            var fileLastWriteTime = await _fileSystem.GetFileLastWriteTimeAsync(filePath, cancellationToken);

            using (var inStream = await _fileSystem.GetFileStreamAsync(filePath, cancellationToken))
                using (var outStream = new RecyclableMemoryStream(_bytesPool))
                {
                    this.ConvertImage(inStream, outStream, options.Width, options.Height, options.ResizeType, options.FormatType);
                    outStream.Seek(0, SeekOrigin.Begin);

                    var image = outStream.ToMemoryOwner();

                    var fileMeta      = new FileMeta(filePath, (ulong)fileLength, Timestamp.FromDateTime(fileLastWriteTime));
                    var thumbnailMeta = new ThumbnailMeta(options.ResizeType, options.FormatType, (uint)options.Width, (uint)options.Height);
                    var content       = new ThumbnailContent(image);
                    var cache         = new ThumbnailCache(fileMeta, thumbnailMeta, new[] { content });

                    await _thumbnailGeneratorRepository.ThumbnailCaches.InsertAsync(cache);

                    return(new ThumbnailGeneratorGetThumbnailResult(ThumbnailGeneratorGetThumbnailResultStatus.Succeeded, cache.Contents));
                }
        }
        catch (NotSupportedException e)
        {
            _logger.Warn(e);
        }
        catch (OperationCanceledException e)
        {
            _logger.Debug(e);
        }
        catch (Exception e)
        {
            _logger.Error(e);
            throw;
        }

        return(new ThumbnailGeneratorGetThumbnailResult(ThumbnailGeneratorGetThumbnailResultStatus.Failed));
    }
Exemple #8
0
    private async ValueTask <ThumbnailGeneratorGetThumbnailResult> GetThumbnailFromCacheAsync(NestedPath filePath, ThumbnailGeneratorGetThumbnailOptions options, CancellationToken cancellationToken = default)
    {
        var cache = await _thumbnailGeneratorRepository.ThumbnailCaches.FindOneAsync(filePath, options.Width, options.Height, options.ResizeType, options.FormatType);

        if (cache is null)
        {
            return(new ThumbnailGeneratorGetThumbnailResult(ThumbnailGeneratorGetThumbnailResultStatus.Failed));
        }

        var fileLength = await _fileSystem.GetFileSizeAsync(filePath, cancellationToken);

        var fileLastWriteTime = await _fileSystem.GetFileLastWriteTimeAsync(filePath, cancellationToken);

        if ((ulong)fileLength != cache.FileMeta.Length &&
            Timestamp.FromDateTime(fileLastWriteTime) != cache.FileMeta.LastWriteTime)
        {
            return(new ThumbnailGeneratorGetThumbnailResult(ThumbnailGeneratorGetThumbnailResultStatus.Failed));
        }

        return(new ThumbnailGeneratorGetThumbnailResult(ThumbnailGeneratorGetThumbnailResultStatus.Succeeded, cache.Contents));
    }
Exemple #9
0
 public PictureWindow(AppState state, NestedPath path)
     : this()
 {
     _state = state;
     _path  = path;
 }