async Task <(string?, Stream?)> GetJsonStreamAsync(ZipArchive zipArchive, string fileName)
        {
            _dotLottieFile = await DotLottieFile.FromZipArchiveAsync(zipArchive);

            if (_dotLottieFile is null)
            {
                return(null, null);
            }

            var firstAnimation = _dotLottieFile.Animations[0];

            return(fileName, firstAnimation.Open());
        }
Exemple #2
0
        static bool ProcessDotLottieFile(
            CommandLineOptions options,
            Reporter reporter,
            string filePath,
            string outputFolder,
            DateTime timestamp)
        {
            using var fileStream = TryOpenFile(filePath, reporter);

            if (fileStream is null)
            {
                return(false);
            }

            ZipArchive zipArchive;

            try
            {
                zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Read);
            }
            catch (InvalidDataException e)
            {
                using (reporter.ErrorStream.Lock())
                {
                    reporter.WriteError($"File {filePath} is not a valid .lottie file.");
                    reporter.WriteError(e.Message);
                }

                return(false);
            }

            var dotLottieFile = DotLottieFile.FromZipArchive(zipArchive);

            if (dotLottieFile is null)
            {
                reporter.WriteError($"File {filePath} is not a valid .lottie file.");
                return(false);
            }

            if (dotLottieFile.Animations.Count == 0)
            {
                reporter.WriteError($"File {filePath} contains no animations.");
                return(false);
            }

            var succeeded = true;

            foreach (var animation in dotLottieFile.Animations)
            {
                var jsonPath = $"{filePath}{animation.Path}";

                using var jsonStream = animation.Open();

                if (jsonStream == null)
                {
                    reporter.WriteError($"Failed to read from {jsonPath}.");
                    succeeded = false;
                    continue;
                }

                // The name of the class should reflect the name of the .lottie file,
                // but if there are multiple .json files in the .lottie file then we
                // need to differentiate them by adding the file name.
                var className = dotLottieFile.Animations.Count == 1
                    ? GetClassName(filePath)
                    : GetClassName($"{filePath}_{animation.Id}");

                if (!LottieJsonFileProcessor.ProcessLottieJsonFile(
                        options: options,
                        reporter: reporter,
                        sourceFilePath: filePath,
                        jsonFilePath: jsonPath,
                        className: className,
                        jsonStream: jsonStream,
                        outputFolder: outputFolder,
                        timestamp))
                {
                    succeeded = false;
                }
            }

            return(succeeded);
        }