Beispiel #1
0
    /// <summary>
    /// Creates asset from specified filePath and loads it with data (Thumbnail, Size, etc.)
    /// </summary>
    /// <param name="filePath">Full path to the image file.</param>
    /// <returns>Created asset.</returns>
    /// <exception cref="ArgumentNullException">If 'filePath' is null or empty.</exception>
    /// <exception cref="FileNotFoundException">If file does not exists or cannot be opened.</exception>
    /// <exception cref="Exception">If creating fails.</exception>
    public Asset Create(string filePath)
    {
        if (string.IsNullOrWhiteSpace(filePath))
        {
            throw new ArgumentNullException(nameof(filePath));
        }

        if (!File.Exists(filePath))
        {
            throw new FileNotFoundException(filePath);
        }

        if (_loadedAssets.ContainsKey(filePath))
        {
            return(_loadedAssets[filePath]);
        }

        Asset asset = new Asset(filePath);

        _assetDataLoader.LoadData(asset);

        _loadedAssets.Add(asset.Path, asset);

        return(asset);
    }
    /// <summary>
    /// Asynchronously loads stored Asset Group from specified json file. Thumbnails included.
    /// </summary>
    /// <param name="filePath">Full path to the group json file.</param>
    /// <returns>Result of the loading. Output will be empty if not successful.</returns>
    public async Task <Result <AssetGroup> > LoadGroupAsync(string filePath)
    {
        AssetGroup?group = await LoadGroupFromFile(filePath);

        if (group is null)
        {
            return(new Result <AssetGroup>(false, new AssetGroup()));
        }

        if (group.Assets.Count == 0)
        {
            _logger.Warning($"[Assset Group Loading] Group '{group.Name}' was loaded, but contains no assets.");
            return(new Result <AssetGroup>(false, group));
        }

        List <Asset> failedAssets = new();

        foreach (var asset in group.Assets)
        {
            try
            {
                if (!File.Exists(asset.Path))
                {
                    _logger.Warning($"[Assset Group Loading] Cannot load asset '{asset.Path}'. File does not exist. Asset will be removed.");
                    failedAssets.Add(asset);
                }
            }
            catch (Exception ex) { _logger.Error($"[Assset Group Loading] Failed to check if file exists: {ex.Message}"); }

            await Task.Run(() => _assetDataLoader.LoadData(asset));
        }

        foreach (var asset in failedAssets)
        {
            group.Assets.Remove(asset);
        }

        return(new Result <AssetGroup>(true, group));
    }