74 lines
1.7 KiB
C#
74 lines
1.7 KiB
C#
using MaybeError;
|
|
using MaybeError.Errors;
|
|
|
|
using MongoDB.Bson;
|
|
using MongoDB.Bson.Serialization.Attributes;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace AZKiServer.Models;
|
|
|
|
public partial class MediaEntry
|
|
{
|
|
[BsonId]
|
|
public ObjectId Id { get; set; }
|
|
public MediaType Type { get; set; }
|
|
public required string Filepath { get; set; }
|
|
public DateTime Date { get; set; }
|
|
public byte CameraId { get; set; }
|
|
|
|
public static Maybe<MediaEntry> Parse(string relativePath)
|
|
{
|
|
var filename = Path.GetFileName(relativePath);
|
|
|
|
var match = FileParser().Match(filename);
|
|
if (!match.Success)
|
|
return new Error("Failed to parse file name");
|
|
|
|
try
|
|
{
|
|
var src = match.Groups["src"];
|
|
var cam = match.Groups["cam"];
|
|
var date = match.Groups["date"];
|
|
var ext = match.Groups["ext"];
|
|
|
|
return new MediaEntry
|
|
{
|
|
CameraId = byte.Parse(cam.Value),
|
|
Filepath = relativePath,
|
|
Date = ParseDate(date.Value),
|
|
Type = ext.Value switch
|
|
{
|
|
"mp4" => MediaType.Video,
|
|
_ => MediaType.Image
|
|
}
|
|
|
|
};
|
|
|
|
}catch(Exception ex)
|
|
{
|
|
return ex;
|
|
}
|
|
}
|
|
|
|
private static DateTime ParseDate(string dateString)
|
|
{
|
|
var year = dateString[0..4];
|
|
var month = dateString[4..6];
|
|
var day = dateString[6..8];
|
|
var hour = dateString[8..10];
|
|
var minute = dateString[10..12];
|
|
var sec = dateString[12..];
|
|
return new DateTime(int.Parse(year), int.Parse(month), int.Parse(day), int.Parse(hour), int.Parse(minute), int.Parse(sec), DateTimeKind.Local);
|
|
}
|
|
|
|
[GeneratedRegex("(?'src'.+)_(?'cam'\\d+)_(?'date'\\d+).(?'ext'\\w+)")]
|
|
private static partial Regex FileParser();
|
|
}
|
|
|
|
|
|
public enum MediaType
|
|
{
|
|
Video,
|
|
Image
|
|
} |