4
4
using OneShelf . Common ;
5
5
using OneShelf . Videos . Database ;
6
6
using OneShelf . Videos . Database . Models ;
7
+ using System . Globalization ;
7
8
8
9
namespace OneShelf . Videos . BusinessLogic . Services ;
9
10
@@ -68,15 +69,15 @@ public async Task SaveInventory()
68
69
_logger . LogInformation ( "Saved." ) ;
69
70
}
70
71
71
- public async Task UploadPhotos ( List < ( long chatId , int messageId , string path , DateTime publishedOn ) > items )
72
+ public async Task UploadPhotos ( List < ( int mediaId , string path , DateTime publishedOn ) > items )
72
73
{
73
74
await _extendedGooglePhotosService . LoginAsync ( ) ;
74
75
75
- var itemsByKey = items . ToDictionary ( x => ( x . chatId , x . messageId ) ) ;
76
- var fileNameTimestamps = new Dictionary < ( long chatId , int messageId ) , DateTime > ( ) ;
76
+ var itemsByKey = items . ToDictionary ( x => x . mediaId ) ;
77
+ var fileNameTimestamps = new Dictionary < int , DateTime > ( ) ;
77
78
var result = await _extendedGooglePhotosService . UploadMultiple (
78
79
items
79
- . Select ( x => ( ( x . chatId , x . messageId ) , x . path , ( string ? ) null ) )
80
+ . Select ( x => ( x . mediaId , x . path , ( string ? ) null ) )
80
81
. ToList ( ) ,
81
82
newItems => AddToDatabase ( itemsByKey , newItems , fileNameTimestamps ) ,
82
83
async ( x , i ) =>
@@ -88,11 +89,13 @@ public async Task UploadPhotos(List<(long chatId, int messageId, string path, Da
88
89
}
89
90
90
91
Directory . CreateDirectory ( tempDirectory ) ;
91
- var tempFileName = Path . Combine ( Path . GetTempPath ( ) , i . ToString ( ) , Path . GetFileName ( itemsByKey [ x ] . path ) ) ;
92
- var timestampFromFile = await _exifService . SetExifTimestamp ( itemsByKey [ x ] . path , tempFileName ) ;
92
+ var fileName = itemsByKey [ x ] . path ;
93
+ var tempFileName = Path . Combine ( Path . GetTempPath ( ) , i . ToString ( ) , Path . GetFileName ( fileName ) ) ;
94
+ var exifTimestamp = DateTime . ParseExact ( Path . GetFileNameWithoutExtension ( fileName ) . SelectSingle ( x => x . Substring ( x . IndexOf ( '@' ) + 1 ) ) , "dd-MM-yyyy_HH-mm-ss" , CultureInfo . InvariantCulture , DateTimeStyles . AssumeUniversal ) ;
95
+ await _exifService . SetExifTimestamp ( fileName , tempFileName , exifTimestamp ) ;
93
96
lock ( fileNameTimestamps )
94
97
{
95
- fileNameTimestamps [ x ] = timestampFromFile ;
98
+ fileNameTimestamps [ x ] = exifTimestamp ;
96
99
}
97
100
98
101
return tempFileName ;
@@ -102,19 +105,19 @@ public async Task UploadPhotos(List<(long chatId, int messageId, string path, Da
102
105
Console . WriteLine ( $ "started: { items . Count } , finished: { result . Count } ") ;
103
106
}
104
107
105
- public async Task UploadVideos ( List < ( long chatId , int messageId , string path , DateTime publishedOn ) > items )
108
+ public async Task UploadVideos ( List < ( int mediaId , string path , DateTime publishedOn ) > items )
106
109
{
107
110
await _extendedGooglePhotosService . LoginAsync ( ) ;
108
111
109
- var added = ( await _videosDatabase . UploadedItems . Select ( x => new { ChatId = x . StaticChatId , MessageId = x . StaticMessageId } ) . ToListAsync ( ) ) . ToHashSet ( ) ;
112
+ var added = ( await _videosDatabase . UploadedItems . Select ( x => x . MediaId ) . ToListAsync ( ) ) . ToHashSet ( ) ;
110
113
Console . WriteLine ( $ "initial items: { items . Count } ") ;
111
- items = items . Where ( x => ! added . Contains ( new { ChatId = x . chatId , MessageId = x . messageId } ) ) . ToList ( ) ;
114
+ items = items . Where ( x => ! added . Contains ( x . mediaId ) ) . ToList ( ) ;
112
115
Console . WriteLine ( $ "remaining items: { items . Count } ") ;
113
116
114
- var itemsByKey = items . ToDictionary ( x => ( x . chatId , x . messageId ) ) ;
117
+ var itemsByKey = items . ToDictionary ( x => x . mediaId ) ;
115
118
var result = await _extendedGooglePhotosService . UploadMultiple (
116
119
items
117
- . Select ( x => ( ( x . chatId , x . messageId ) , x . path ,
120
+ . Select ( x => ( x . mediaId , x . path ,
118
121
//$"chatId = {x.chatId}, messageId = {x.messageId}, published on = {x.publishedOn}, filename = {Path.GetFileName(x.path)}"
119
122
( string ? ) null ) )
120
123
. ToList ( ) ,
@@ -126,22 +129,22 @@ public async Task UploadVideos(List<(long chatId, int messageId, string path, Da
126
129
}
127
130
128
131
private async Task AddToDatabase (
129
- Dictionary < ( long chatId , int messageId ) , ( long chatId , int messageId , string path , DateTime publishedOn ) > items ,
130
- Dictionary < ( long chatId , int messageId ) , NewMediaItemResult > newItems ,
131
- Dictionary < ( long chatId , int messageId ) , DateTime > ? fileNameTimestamps = null )
132
+ Dictionary < int , ( int mediaId , string path , DateTime publishedOn ) > items ,
133
+ Dictionary < int , NewMediaItemResult > newItems ,
134
+ Dictionary < int , DateTime > ? fileNameTimestamps = null )
132
135
{
133
- _videosDatabaseOperations . AddItems ( newItems . Select ( i => items [ i . Key ] . SelectSingle ( x => ( x . chatId , x . messageId , x . path , x . publishedOn , result : i . Value , fileNameTimestamp : fileNameTimestamps ? [ i . Key ] ) ) ) ) ;
136
+ _videosDatabaseOperations . AddItems ( newItems . Select ( i => items [ i . Key ] . SelectSingle ( x => ( x . mediaId , x . path , x . publishedOn , result : i . Value , fileNameTimestamp : fileNameTimestamps ? [ i . Key ] ) ) ) ) ;
134
137
await _videosDatabase . SaveChangesAsync ( ) ;
135
138
}
136
139
137
- public async Task CreateAlbums ( List < ( int albumId , string title , List < ( string ? mediaItemId , long chatId , int messageId ) > items ) > albums )
140
+ public async Task CreateAlbums ( List < ( int albumId , string title , List < ( string mediaItemId , int mediaId ) > items ) > albums )
138
141
{
139
142
await _extendedGooglePhotosService . LoginAsync ( ) ;
140
143
foreach ( var ( albumId , title , items ) in albums )
141
144
{
142
145
_logger . LogInformation ( "{title} uploading..." , title ) ;
143
146
var googleAlbum = await _extendedGooglePhotosService . CreateAlbumAsync ( title ) ;
144
- await _extendedGooglePhotosService . AddMediaItemsToAlbumWithRetryAsync ( googleAlbum ! . id , items . Where ( x => x . mediaItemId != null ) . Select ( x => x . mediaItemId ! ) . ToList ( ) ) ;
147
+ await _extendedGooglePhotosService . AddMediaItemsToAlbumWithRetryAsync ( googleAlbum ! . id , items . Select ( x => x . mediaItemId ) . ToList ( ) ) ;
145
148
146
149
_videosDatabase . UploadedAlbums . Add ( new ( )
147
150
{
0 commit comments