Avoid unnecessary Auth_ExportAuthorization. More reliable DownloadFileAsync

This commit is contained in:
Wizou 2021-09-25 19:43:39 +02:00
parent ca1c1ce8de
commit c789308d1e
2 changed files with 27 additions and 16 deletions

View file

@ -170,7 +170,7 @@ namespace WTelegram
if (DCSession.DataCenter?.id == dcId) return; if (DCSession.DataCenter?.id == dcId) return;
Helpers.Log(2, $"Migrate to DC {dcId}..."); Helpers.Log(2, $"Migrate to DC {dcId}...");
Auth_ExportedAuthorization exported = null; Auth_ExportedAuthorization exported = null;
if (_session.User != null && DCSession.DataCenter.id == _session.MainDC) if (_session.User != null && DCSession.DataCenter.id == _session.MainDC && _session.DCSessions.GetValueOrDefault(dcId)?.UserId != _session.User.id)
exported = await this.Auth_ExportAuthorization(dcId); exported = await this.Auth_ExportAuthorization(dcId);
if (CheckMsgsToAck() is MsgsAck msgsAck) if (CheckMsgsToAck() is MsgsAck msgsAck)
await SendAsync(MakeFunction(msgsAck), false); await SendAsync(MakeFunction(msgsAck), false);
@ -190,6 +190,7 @@ namespace WTelegram
if (authorization is not Auth_Authorization { user: User user }) if (authorization is not Auth_Authorization { user: User user })
throw new ApplicationException("Failed to get Authorization: " + authorization.GetType().Name); throw new ApplicationException("Failed to get Authorization: " + authorization.GetType().Name);
_session.User = user; _session.User = user;
DCSession.UserId = user.id;
} }
} }
@ -728,6 +729,7 @@ namespace WTelegram
if (authorization is not Auth_Authorization { user: User user }) if (authorization is not Auth_Authorization { user: User user })
throw new ApplicationException("Failed to get Authorization: " + authorization.GetType().Name); throw new ApplicationException("Failed to get Authorization: " + authorization.GetType().Name);
_session.User = user; _session.User = user;
DCSession.UserId = user.id;
_session.Save(); _session.Save();
return user; return user;
} }
@ -767,6 +769,7 @@ namespace WTelegram
Helpers.Log(4, $"Error deserializing User! ({ex.Message}) Proceeding to login..."); Helpers.Log(4, $"Error deserializing User! ({ex.Message}) Proceeding to login...");
} }
await this.Auth_LogOut(); await this.Auth_LogOut();
_session.User = null;
} }
phone_number ??= Config("phone_number"); phone_number ??= Config("phone_number");
var sentCode = await this.Auth_SendCode(phone_number, _apiId, _apiHash, settings ?? new()); var sentCode = await this.Auth_SendCode(phone_number, _apiId, _apiHash, settings ?? new());
@ -799,6 +802,7 @@ namespace WTelegram
throw new ApplicationException("Failed to get Authorization: " + authorization.GetType().Name); throw new ApplicationException("Failed to get Authorization: " + authorization.GetType().Name);
//TODO: find better serialization for User not subject to TL changes? //TODO: find better serialization for User not subject to TL changes?
_session.User = user; _session.User = user;
DCSession.UserId = user.id;
_session.Save(); _session.Save();
return user; return user;
} }
@ -913,8 +917,7 @@ namespace WTelegram
public async Task<Storage_FileType> DownloadFileAsync(Photo photo, Stream outputStream, PhotoSizeBase photoSize = null) public async Task<Storage_FileType> DownloadFileAsync(Photo photo, Stream outputStream, PhotoSizeBase photoSize = null)
{ {
var fileLocation = photo.ToFileLocation(photoSize ?? photo.LargestPhotoSize); var fileLocation = photo.ToFileLocation(photoSize ?? photo.LargestPhotoSize);
await MigrateDCAsync(photo.dc_id); return await DownloadFileAsync(fileLocation, outputStream, photo.dc_id);
return await DownloadFileAsync(fileLocation, outputStream);
} }
/// <summary>Download given photo from Telegram into the outputStream</summary> /// <summary>Download given photo from Telegram into the outputStream</summary>
@ -923,29 +926,36 @@ namespace WTelegram
public async Task<Storage_FileType> DownloadFileAsync(Document document, Stream outputStream, PhotoSizeBase thumbSize = null) public async Task<Storage_FileType> DownloadFileAsync(Document document, Stream outputStream, PhotoSizeBase thumbSize = null)
{ {
var fileLocation = document.ToFileLocation(thumbSize); var fileLocation = document.ToFileLocation(thumbSize);
await MigrateDCAsync(document.dc_id); return await DownloadFileAsync(fileLocation, outputStream, document.dc_id);
return await DownloadFileAsync(fileLocation, outputStream);
} }
/// <summary>Download given file from Telegram into the outputStream</summary> /// <summary>Download given file from Telegram into the outputStream</summary>
/// <param name="fileLocation">Telegram file identifier, typically obtained with a .ToFileLocation() call</param> /// <param name="fileLocation">Telegram file identifier, typically obtained with a .ToFileLocation() call</param>
/// <param name="outputStream">stream to write to. This method does not close/dispose the stream</param> /// <param name="outputStream">stream to write to. This method does not close/dispose the stream</param>
public async Task<Storage_FileType> DownloadFileAsync(InputFileLocationBase fileLocation, Stream outputStream) /// <param name="fileDC">(optional) DC on which the file is stored</param>
public async Task<Storage_FileType> DownloadFileAsync(InputFileLocationBase fileLocation, Stream outputStream, int fileDC = 0)
{ {
const int ChunkSize = 128 * 1024; const int ChunkSize = 128 * 1024;
int fileSize = 0; int fileSize = 0;
Upload_File fileData; Upload_File fileData;
do try
{ {
var fileBase = await this.Upload_GetFile(fileLocation, fileSize, ChunkSize); if (fileDC != 0) await MigrateDCAsync(fileDC);
fileData = fileBase as Upload_File; do
if (fileData == null) {
throw new ApplicationException("Upload_GetFile returned unsupported " + fileBase.GetType().Name); var fileBase = await this.Upload_GetFile(fileLocation, fileSize, ChunkSize);
await outputStream.WriteAsync(fileData.bytes, 0, fileData.bytes.Length); fileData = fileBase as Upload_File;
fileSize += fileData.bytes.Length; if (fileData == null)
throw new ApplicationException("Upload_GetFile returned unsupported " + fileBase.GetType().Name);
} while (fileData.bytes.Length == ChunkSize); await outputStream.WriteAsync(fileData.bytes, 0, fileData.bytes.Length);
await MigrateDCAsync(); // migrate back to main DC fileSize += fileData.bytes.Length;
} while (fileData.bytes.Length == ChunkSize);
}
finally
{
await MigrateDCAsync(); // migrate back to main DC
}
return fileData.type; return fileData.type;
} }
#endregion #endregion

View file

@ -19,6 +19,7 @@ namespace WTelegram
{ {
public long AuthKeyID; public long AuthKeyID;
public byte[] AuthKey; // 2048-bit = 256 bytes public byte[] AuthKey; // 2048-bit = 256 bytes
public long UserId;
public long Salt; public long Salt;
public int Seqno; public int Seqno;
public long ServerTicksOffset; public long ServerTicksOffset;