Compare commits

..

No commits in common. "fa42ea67ac1b77cbc6e804e3d3044ce0336557ff" and "b9fc1907fc2c9e9b7b0bc19237b444d8e8a1eb54" have entirely different histories.

View File

@ -14,14 +14,46 @@ namespace DysonNetwork.Sphere.Storage;
public class FileService(AppDatabase db, IConfiguration configuration) public class FileService(AppDatabase db, IConfiguration configuration)
{ {
// The analysis file method no longer will remove the GPS EXIF data private static readonly List<ExifTag> BlacklistExifTags =
// It should be handled on the client side, and for some specific cases it should be keep [
public async Task<CloudFile> AnalyzeFileAsync( ExifTag.GPSLatitudeRef,
ExifTag.GPSLatitude,
ExifTag.GPSLongitudeRef,
ExifTag.GPSLongitude,
ExifTag.GPSAltitudeRef,
ExifTag.GPSAltitude,
ExifTag.GPSSatellites,
ExifTag.GPSStatus,
ExifTag.GPSMeasureMode,
ExifTag.GPSDOP,
ExifTag.GPSSpeedRef,
ExifTag.GPSSpeed,
ExifTag.GPSTrackRef,
ExifTag.GPSTrack,
ExifTag.GPSImgDirectionRef,
ExifTag.GPSImgDirection,
ExifTag.GPSMapDatum,
ExifTag.GPSDestLatitudeRef,
ExifTag.GPSDestLatitude,
ExifTag.GPSDestLongitudeRef,
ExifTag.GPSDestLongitude,
ExifTag.GPSDestBearingRef,
ExifTag.GPSDestBearing,
ExifTag.GPSDestDistanceRef,
ExifTag.GPSDestDistance,
ExifTag.GPSProcessingMethod,
ExifTag.GPSAreaInformation,
ExifTag.GPSDateStamp,
ExifTag.GPSDifferential
];
public async Task<(CloudFile, Stream)> AnalyzeFileAsync(
Account.Account account, Account.Account account,
string fileId, string fileId,
Stream stream, Stream stream,
string fileName, string fileName,
string? contentType string? contentType,
string? filePath = null
) )
{ {
var fileSize = stream.Length; var fileSize = stream.Length;
@ -29,7 +61,7 @@ public class FileService(AppDatabase db, IConfiguration configuration)
contentType ??= !fileName.Contains('.') ? "application/octet-stream" : MimeTypes.GetMimeType(fileName); contentType ??= !fileName.Contains('.') ? "application/octet-stream" : MimeTypes.GetMimeType(fileName);
var existingFile = await db.Files.Where(f => f.Hash == hash).FirstOrDefaultAsync(); var existingFile = await db.Files.Where(f => f.Hash == hash).FirstOrDefaultAsync();
if (existingFile is not null) return existingFile; if (existingFile is not null) return (existingFile, stream);
var file = new CloudFile var file = new CloudFile
{ {
@ -56,9 +88,16 @@ public class FileService(AppDatabase db, IConfiguration configuration)
ushort orientation = 1; ushort orientation = 1;
List<IExifValue> exif = []; List<IExifValue> exif = [];
if (exifProfile?.Values.FirstOrDefault(e => e.Tag == ExifTag.Orientation) if (exifProfile is not null)
?.GetValue() is ushort o) {
orientation = o; exif = exifProfile.Values
.Where(v => !BlacklistExifTags.Contains((ExifTag)v.Tag))
.ToList<IExifValue>();
if (exifProfile.Values.FirstOrDefault(e => e.Tag == ExifTag.Orientation)
?.GetValue() is ushort o)
orientation = o;
}
if (orientation is 6 or 8) if (orientation is 6 or 8)
(width, height) = (height, width); (width, height) = (height, width);
@ -75,6 +114,11 @@ public class FileService(AppDatabase db, IConfiguration configuration)
["ratio"] = aspectRatio, ["ratio"] = aspectRatio,
["exif"] = exif ["exif"] = exif
}; };
var newStream = new MemoryStream();
await imageSharp.SaveAsWebpAsync(newStream);
file.MimeType = "image/webp";
stream = newStream;
} }
break; break;
@ -96,7 +140,7 @@ public class FileService(AppDatabase db, IConfiguration configuration)
db.Files.Add(file); db.Files.Add(file);
await db.SaveChangesAsync(); await db.SaveChangesAsync();
return file; return (file, stream);
} }
private static async Task<string> HashFileAsync(Stream stream, int chunkSize = 1024 * 1024, long? fileSize = null) private static async Task<string> HashFileAsync(Stream stream, int chunkSize = 1024 * 1024, long? fileSize = null)