Skip to content

Commit

Permalink
uploading photos and videos seems to work correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-zhur committed Sep 29, 2024
1 parent 46eb6f9 commit 3b82926
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
17 changes: 13 additions & 4 deletions OneShelf.Videos/OneShelf.Videos.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@
var service2 = host.Services.GetRequiredService<Service2>();
var service3 = host.Services.GetRequiredService<ExifService>();
var service4 = host.Services.GetRequiredService<LiveDownloader>();

await using var videosDatabase = host.Services.GetRequiredService<VideosDatabase>();
await videosDatabase.Database.MigrateAsync();

await videosDatabase.AppendTopics();
//await service4.Try(false);
//await videosDatabase.Database.MigrateAsync();

//await service4.UpdateLive(true);
//await videosDatabase.AppendTopics();
//await videosDatabase.AppendMediae();
//await videosDatabase.UpdateMediaTopics();



await service2.UploadPhotos(await service1.GetExportLivePhoto());
await service2.UploadVideos(await service1.GetExportLiveVideo());



//await videosDatabase.CreateMissingStaticTopics();
//await videosDatabase.UpdateStaticMessagesTopics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ namespace OneShelf.Videos.BusinessLogic.Services;

public class ExifService
{
public async Task SetExifTimestamp(string path, string tempFileName, DateTime timestamp)
public async Task<bool> SetExifTimestamp(string path, string tempFileName, DateTime timestamp)
{
var image = await ImageFile.FromFileAsync(path);
if (image.Errors.Any()) throw new($"Some image errors, {path}.");
if (image.Properties.Contains(ExifTag.DateTime)) throw new($"The image contains a datetime, {path}.");
if (image.Properties.Contains(ExifTag.DateTime))
{
File.Copy(path, tempFileName);
return false;
}

image.Properties.Set(ExifTag.DateTime, timestamp);
await image.SaveAsync(tempFileName);

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class LiveDownloader(IOptions<VideosOptions> options, ILogger<LiveDownloa
private byte[]? _session;
private readonly AsyncLock _databaseLock = new();

public async Task Try(bool downloadMissing)
public async Task UpdateLive(bool downloadMissing)
{
await using var client = await Login();

Expand Down Expand Up @@ -47,7 +47,7 @@ public async Task Try(bool downloadMissing)

var topics = await GetTopicsAndMedia(messages);

await Save(chat, topics);
await SaveToDatabase(chat, topics);

return topics;
}
Expand Down Expand Up @@ -84,7 +84,7 @@ await Parallel.ForEachAsync(
await client.DownloadFileAsync(photo, outputStream);
}

var fileName = liveMedia.FileName != null && !string.IsNullOrWhiteSpace(Path.GetExtension(liveMedia.FileName)) ? $"{liveMedia.MediaId}{Path.GetExtension(liveMedia.FileName)}" : liveMedia.MediaId.ToString();
var fileName = liveMedia.FileName != null && !string.IsNullOrWhiteSpace(Path.GetExtension(liveMedia.FileName)) ? $"{liveMedia.MediaId}{Path.GetExtension(liveMedia.FileName)}" : $"{liveMedia.MediaId}.{(photo != null ? "jpg" : "mp4")}";
await File.WriteAllBytesAsync(
paths.GetLiveDownloadedPath(fileName),
outputStream.ToArray(), cancellationToken);
Expand Down Expand Up @@ -134,7 +134,7 @@ await File.WriteAllBytesAsync(
});
}

private async Task Save(ChatBase chat, Dictionary<int, (string name, List<(Document? document, Photo? photo, Message message, string mediaFlags)> media)> topics)
private async Task SaveToDatabase(ChatBase chat, Dictionary<int, (string name, List<(Document? document, Photo? photo, Message message, string mediaFlags)> media)> topics)
{
var liveChat = await videosDatabase.LiveChats.Include(x => x.LiveTopics).ThenInclude(x => x.LiveMediae).SingleOrDefaultAsync(x => x.Id == chat.ID);
if (liveChat == null)
Expand Down

0 comments on commit 3b82926

Please sign in to comment.