Beispiel #1
0
        public IngestionItem WithDurationAndFileSizeByReadingFile(string?basePath = null)
        {
            var fullPath = basePath is null
                ? FilePath
                : Path.Combine(basePath, FilePath);

            return(new IngestionItem(
                       FilePath,
                       new FileInfo(fullPath).Length,
                       Tag,
                       Timestamp,
                       FFMpeg.ParseDuration(fullPath)));
        }
Beispiel #2
0
        public static IngestionItem FromFileSystem(
            string?basePath,
            string fullPath,
            Match?metadataMatch,
            bool parseDuration = true)
        {
            var path = basePath is null
                ? fullPath
                : fullPath.Substring(basePath.Length + 1);

            var fileSize = new FileInfo(fullPath).Length;

            if (metadataMatch is null)
            {
                return(new IngestionItem(path, fileSize));
            }

            var tzString = Match("zzzz", "zzz", "zz", "z");
            var tzOffset = TimeZoneInfo.Local.BaseUtcOffset;

            if (tzString is string)
            {
                tzString = tzString.Replace(":", string.Empty);
                if (int.TryParse(tzString, out var tzInt))
                {
                    tzOffset = TimeSpan
                               .FromHours(tzInt / 100)
                               .Add(TimeSpan.FromMinutes(Math.Abs(tzInt % 100)));
                }
                else
                {
                    tzOffset = TimeZoneInfo
                               .FindSystemTimeZoneById(tzString)
                               .BaseUtcOffset;
                }
            }

            var timestamp = new DateTimeOffset(
                MatchInt("yyyyy", "yyyy", "yyy", "yy") ?? 1,
                MatchInt("MM", "M") ?? 1,
                MatchInt("dd", "d") ?? 1,
                MatchInt("HH", "H") ?? 0,
                MatchInt("mm", "m") ?? 0,
                MatchInt("ss", "s") ?? 0,
                tzOffset);

            TimeSpan?duration = null;

            if (parseDuration)
            {
                duration = FFMpeg.ParseDuration(fullPath);
            }

            return(new IngestionItem(
                       path,
                       fileSize,
                       Match("tag"),
                       timestamp,
                       duration));

            string?Match(params string[] keys)
            {
                // FIXME: Roslyn: this should never be null
                if (metadataMatch is null)
                {
                    return(null);
                }

                foreach (var key in keys)
                {
                    if (metadataMatch.Groups.TryGetValue(key, out var group) && group.Success)
                    {
                        return(group.Value);
                    }
                }

                return(null);
            }

            int?MatchInt(params string[] keys)
            {
                // FIXME: Roslyn: this should never be null
                if (metadataMatch is null)
                {
                    return(null);
                }

                foreach (var key in keys)
                {
                    if (metadataMatch.Groups.TryGetValue(key, out var group) &&
                        group.Success &&
                        int.TryParse(group.Value, out var intValue))
                    {
                        return(intValue);
                    }
                }
                return(null);
            }
        }