2014-05-30 08:53:19 +00:00
/*
This file is part of Telegram Desktop ,
2014-12-01 10:47:38 +00:00
the official desktop version of Telegram messaging app , see https : //telegram.org
2014-05-30 08:53:19 +00:00
Telegram Desktop is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
It is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
2015-10-03 13:16:42 +00:00
In addition , as a special exception , the copyright holders give permission
to link the code of portions of this program with the OpenSSL library .
2014-05-30 08:53:19 +00:00
Full license : https : //github.com/telegramdesktop/tdesktop/blob/master/LICENSE
2016-02-08 10:56:18 +00:00
Copyright ( c ) 2014 - 2016 John Preston , https : //desktop.telegram.org
2014-05-30 08:53:19 +00:00
*/
# include "stdafx.h"
# include "fileuploader.h"
2015-09-03 10:48:40 +00:00
FileUploader : : FileUploader ( ) : sentSize ( 0 ) {
2014-10-30 16:23:44 +00:00
memset ( sentSizes , 0 , sizeof ( sentSizes ) ) ;
2014-05-30 08:53:19 +00:00
nextTimer . setSingleShot ( true ) ;
connect ( & nextTimer , SIGNAL ( timeout ( ) ) , this , SLOT ( sendNext ( ) ) ) ;
2014-10-30 16:23:44 +00:00
killSessionsTimer . setSingleShot ( true ) ;
connect ( & killSessionsTimer , SIGNAL ( timeout ( ) ) , this , SLOT ( killSessions ( ) ) ) ;
2014-05-30 08:53:19 +00:00
}
2015-09-03 10:48:40 +00:00
void FileUploader : : uploadMedia ( const FullMsgId & msgId , const ReadyLocalMedia & media ) {
2015-10-27 02:39:02 +00:00
if ( media . type = = PreparePhoto ) {
2014-05-30 08:53:19 +00:00
App : : feedPhoto ( media . photo , media . photoThumbs ) ;
2016-02-12 16:35:06 +00:00
} else if ( media . type = = PrepareDocument | | media . type = = PrepareAudio ) {
2014-05-30 08:53:19 +00:00
DocumentData * document ;
if ( media . photoThumbs . isEmpty ( ) ) {
2015-01-02 14:55:24 +00:00
document = App : : feedDocument ( media . document ) ;
2014-05-30 08:53:19 +00:00
} else {
2015-01-02 14:55:24 +00:00
document = App : : feedDocument ( media . document , media . photoThumbs . begin ( ) . value ( ) ) ;
2014-05-30 08:53:19 +00:00
}
document - > status = FileUploading ;
2016-02-12 16:35:06 +00:00
if ( ! media . data . isEmpty ( ) ) {
document - > setData ( media . data ) ;
}
2014-05-30 08:53:19 +00:00
if ( ! media . file . isEmpty ( ) ) {
2015-11-26 17:34:52 +00:00
document - > setLocation ( FileLocation ( StorageFilePartial , media . file ) ) ;
2014-05-30 08:53:19 +00:00
}
}
queue . insert ( msgId , File ( media ) ) ;
sendNext ( ) ;
}
2015-10-28 02:41:13 +00:00
void FileUploader : : upload ( const FullMsgId & msgId , const FileLoadResultPtr & file ) {
2015-10-27 02:39:02 +00:00
if ( file - > type = = PreparePhoto ) {
2015-12-24 19:26:28 +00:00
PhotoData * photo = App : : feedPhoto ( file - > photo , file - > photoThumbs ) ;
photo - > uploadingData = new PhotoData : : UploadingData ( file - > partssize ) ;
2016-02-12 16:35:06 +00:00
} else if ( file - > type = = PrepareDocument | | file - > type = = PrepareAudio ) {
2015-10-27 02:39:02 +00:00
DocumentData * document ;
if ( file - > thumb . isNull ( ) ) {
document = App : : feedDocument ( file - > document ) ;
} else {
document = App : : feedDocument ( file - > document , file - > thumb ) ;
}
document - > status = FileUploading ;
2016-02-12 16:35:06 +00:00
if ( ! file - > content . isEmpty ( ) ) {
document - > setData ( file - > content ) ;
}
2015-10-27 02:39:02 +00:00
if ( ! file - > filepath . isEmpty ( ) ) {
2015-11-26 17:34:52 +00:00
document - > setLocation ( FileLocation ( StorageFilePartial , file - > filepath ) ) ;
2015-10-27 02:39:02 +00:00
}
}
queue . insert ( msgId , File ( file ) ) ;
sendNext ( ) ;
}
2014-05-30 08:53:19 +00:00
void FileUploader : : currentFailed ( ) {
Queue : : iterator j = queue . find ( uploading ) ;
if ( j ! = queue . end ( ) ) {
2015-10-27 02:39:02 +00:00
if ( j - > type ( ) = = PreparePhoto ) {
2014-05-30 08:53:19 +00:00
emit photoFailed ( j . key ( ) ) ;
2015-10-27 02:39:02 +00:00
} else if ( j - > type ( ) = = PrepareDocument ) {
DocumentData * doc = App : : document ( j - > id ( ) ) ;
2014-05-30 08:53:19 +00:00
if ( doc - > status = = FileUploading ) {
2015-12-08 19:07:50 +00:00
doc - > status = FileUploadFailed ;
2014-05-30 08:53:19 +00:00
}
emit documentFailed ( j . key ( ) ) ;
}
queue . erase ( j ) ;
}
requestsSent . clear ( ) ;
docRequestsSent . clear ( ) ;
2014-10-30 16:23:44 +00:00
dcMap . clear ( ) ;
2015-09-03 10:48:40 +00:00
uploading = FullMsgId ( ) ;
2014-05-30 08:53:19 +00:00
sentSize = 0 ;
2014-10-30 16:23:44 +00:00
for ( int i = 0 ; i < MTPUploadSessionsCount ; + + i ) {
sentSizes [ i ] = 0 ;
}
2014-05-30 08:53:19 +00:00
sendNext ( ) ;
}
2014-10-30 16:23:44 +00:00
void FileUploader : : killSessions ( ) {
for ( int i = 0 ; i < MTPUploadSessionsCount ; + + i ) {
2014-12-05 13:44:27 +00:00
MTP : : stopSession ( MTP : : upl [ i ] ) ;
2014-10-30 16:23:44 +00:00
}
}
2014-05-30 08:53:19 +00:00
void FileUploader : : sendNext ( ) {
2015-12-24 19:26:28 +00:00
if ( sentSize > = MaxUploadFileParallelSize | | _paused . msg ) return ;
2014-10-30 16:23:44 +00:00
bool killing = killSessionsTimer . isActive ( ) ;
if ( queue . isEmpty ( ) ) {
if ( ! killing ) {
2014-11-05 17:43:32 +00:00
killSessionsTimer . start ( MTPAckSendWaiting + MTPKillFileSessionTimeout ) ;
2014-10-30 16:23:44 +00:00
}
return ;
}
2014-05-30 08:53:19 +00:00
2014-10-30 16:23:44 +00:00
if ( killing ) {
killSessionsTimer . stop ( ) ;
}
2015-09-03 10:48:40 +00:00
Queue : : iterator i = uploading . msg ? queue . find ( uploading ) : queue . begin ( ) ;
if ( ! uploading . msg ) {
2014-05-30 08:53:19 +00:00
uploading = i . key ( ) ;
} else if ( i = = queue . end ( ) ) {
2016-02-12 16:35:06 +00:00
i = queue . begin ( ) ;
2014-05-30 08:53:19 +00:00
uploading = i . key ( ) ;
}
2014-10-30 16:23:44 +00:00
int todc = 0 ;
for ( int dc = 1 ; dc < MTPUploadSessionsCount ; + + dc ) {
if ( sentSizes [ dc ] < sentSizes [ todc ] ) {
todc = dc ;
}
}
2015-10-27 02:39:02 +00:00
UploadFileParts & parts ( i - > file ? ( i - > type ( ) = = PreparePhoto ? i - > file - > fileparts : i - > file - > thumbparts ) : i - > media . parts ) ;
uint64 partsOfId ( i - > file ? ( i - > type ( ) = = PreparePhoto ? i - > file - > id : i - > file - > thumbId ) : i - > media . thumbId ) ;
if ( parts . isEmpty ( ) ) {
2014-05-30 08:53:19 +00:00
if ( i - > docSentParts > = i - > docPartsCount ) {
if ( requestsSent . isEmpty ( ) & & docRequestsSent . isEmpty ( ) ) {
2015-10-27 02:39:02 +00:00
if ( i - > type ( ) = = PreparePhoto ) {
emit photoReady ( uploading , MTP_inputFile ( MTP_long ( i - > id ( ) ) , MTP_int ( i - > partsCount ) , MTP_string ( i - > filename ( ) ) , MTP_string ( i - > file ? i - > file - > filemd5 : i - > media . jpeg_md5 ) ) ) ;
2016-02-12 16:35:06 +00:00
} else if ( i - > type ( ) = = PrepareDocument | | i - > type ( ) = = PrepareAudio ) {
2014-05-30 08:53:19 +00:00
QByteArray docMd5 ( 32 , Qt : : Uninitialized ) ;
2015-05-29 18:52:43 +00:00
hashMd5Hex ( i - > md5Hash . result ( ) , docMd5 . data ( ) ) ;
2014-05-30 08:53:19 +00:00
2015-10-27 02:39:02 +00:00
MTPInputFile doc = ( i - > docSize > UseBigFilesFrom ) ? MTP_inputFileBig ( MTP_long ( i - > id ( ) ) , MTP_int ( i - > docPartsCount ) , MTP_string ( i - > filename ( ) ) ) : MTP_inputFile ( MTP_long ( i - > id ( ) ) , MTP_int ( i - > docPartsCount ) , MTP_string ( i - > filename ( ) ) , MTP_string ( docMd5 ) ) ;
2014-05-30 08:53:19 +00:00
if ( i - > partsCount ) {
2015-10-27 02:39:02 +00:00
emit thumbDocumentReady ( uploading , doc , MTP_inputFile ( MTP_long ( i - > thumbId ( ) ) , MTP_int ( i - > partsCount ) , MTP_string ( i - > file ? i - > file - > thumbname : ( qsl ( " thumb. " ) + i - > media . thumbExt ) ) , MTP_string ( i - > file ? i - > file - > thumbmd5 : i - > media . jpeg_md5 ) ) ) ;
2014-05-30 08:53:19 +00:00
} else {
emit documentReady ( uploading , doc ) ;
}
}
queue . remove ( uploading ) ;
2015-09-03 10:48:40 +00:00
uploading = FullMsgId ( ) ;
2014-05-30 08:53:19 +00:00
sendNext ( ) ;
}
return ;
}
2015-10-27 02:39:02 +00:00
QByteArray & content ( i - > file ? i - > file - > content : i - > media . data ) ;
2014-05-30 08:53:19 +00:00
QByteArray toSend ;
2015-10-27 02:39:02 +00:00
if ( content . isEmpty ( ) ) {
2014-05-30 08:53:19 +00:00
if ( ! i - > docFile ) {
2015-10-27 02:39:02 +00:00
i - > docFile . reset ( new QFile ( i - > file ? i - > file - > filepath : i - > media . file ) ) ;
2014-05-30 08:53:19 +00:00
if ( ! i - > docFile - > open ( QIODevice : : ReadOnly ) ) {
currentFailed ( ) ;
return ;
}
}
toSend = i - > docFile - > read ( i - > docPartSize ) ;
if ( i - > docSize < = UseBigFilesFrom ) {
2015-05-29 18:52:43 +00:00
i - > md5Hash . feed ( toSend . constData ( ) , toSend . size ( ) ) ;
2014-05-30 08:53:19 +00:00
}
} else {
2015-10-27 02:39:02 +00:00
toSend = content . mid ( i - > docSentParts * i - > docPartSize , i - > docPartSize ) ;
if ( ( i - > type ( ) = = PrepareDocument | | i - > type ( ) = = PrepareAudio ) & & i - > docSentParts < = UseBigFilesFrom ) {
2015-05-29 18:52:43 +00:00
i - > md5Hash . feed ( toSend . constData ( ) , toSend . size ( ) ) ;
2014-07-04 11:12:54 +00:00
}
2014-05-30 08:53:19 +00:00
}
2014-06-16 09:31:10 +00:00
if ( toSend . size ( ) > i - > docPartSize | | ( toSend . size ( ) < i - > docPartSize & & i - > docSentParts + 1 ! = i - > docPartsCount ) ) {
2014-05-30 08:53:19 +00:00
currentFailed ( ) ;
return ;
}
mtpRequestId requestId ;
if ( i - > docSize > UseBigFilesFrom ) {
2015-10-27 02:39:02 +00:00
requestId = MTP : : send ( MTPupload_SaveBigFilePart ( MTP_long ( i - > id ( ) ) , MTP_int ( i - > docSentParts ) , MTP_int ( i - > docPartsCount ) , MTP_string ( toSend ) ) , rpcDone ( & FileUploader : : partLoaded ) , rpcFail ( & FileUploader : : partFailed ) , MTP : : upl [ todc ] ) ;
2014-05-30 08:53:19 +00:00
} else {
2015-10-27 02:39:02 +00:00
requestId = MTP : : send ( MTPupload_SaveFilePart ( MTP_long ( i - > id ( ) ) , MTP_int ( i - > docSentParts ) , MTP_string ( toSend ) ) , rpcDone ( & FileUploader : : partLoaded ) , rpcFail ( & FileUploader : : partFailed ) , MTP : : upl [ todc ] ) ;
2014-05-30 08:53:19 +00:00
}
docRequestsSent . insert ( requestId , i - > docSentParts ) ;
2014-10-30 16:23:44 +00:00
dcMap . insert ( requestId , todc ) ;
2014-05-30 08:53:19 +00:00
sentSize + = i - > docPartSize ;
2014-10-30 16:23:44 +00:00
sentSizes [ todc ] + = i - > docPartSize ;
2014-05-30 08:53:19 +00:00
i - > docSentParts + + ;
} else {
2015-10-27 02:39:02 +00:00
UploadFileParts : : iterator part = parts . begin ( ) ;
2016-02-12 16:35:06 +00:00
2015-10-27 02:39:02 +00:00
mtpRequestId requestId = MTP : : send ( MTPupload_SaveFilePart ( MTP_long ( partsOfId ) , MTP_int ( part . key ( ) ) , MTP_string ( part . value ( ) ) ) , rpcDone ( & FileUploader : : partLoaded ) , rpcFail ( & FileUploader : : partFailed ) , MTP : : upl [ todc ] ) ;
2014-05-30 08:53:19 +00:00
requestsSent . insert ( requestId , part . value ( ) ) ;
2014-10-30 16:23:44 +00:00
dcMap . insert ( requestId , todc ) ;
2014-05-30 08:53:19 +00:00
sentSize + = part . value ( ) . size ( ) ;
2014-10-30 16:23:44 +00:00
sentSizes [ todc ] + = part . value ( ) . size ( ) ;
2014-05-30 08:53:19 +00:00
2015-10-27 02:39:02 +00:00
parts . erase ( part ) ;
2014-05-30 08:53:19 +00:00
}
nextTimer . start ( UploadRequestInterval ) ;
}
2015-09-03 10:48:40 +00:00
void FileUploader : : cancel ( const FullMsgId & msgId ) {
2014-05-30 08:53:19 +00:00
uploaded . remove ( msgId ) ;
if ( uploading = = msgId ) {
currentFailed ( ) ;
} else {
queue . remove ( msgId ) ;
}
}
2015-12-24 19:26:28 +00:00
void FileUploader : : pause ( const FullMsgId & msgId ) {
_paused = msgId ;
}
void FileUploader : : unpause ( ) {
_paused = FullMsgId ( ) ;
sendNext ( ) ;
}
2015-09-03 10:48:40 +00:00
void FileUploader : : confirm ( const FullMsgId & msgId ) {
2014-05-30 08:53:19 +00:00
}
void FileUploader : : clear ( ) {
uploaded . clear ( ) ;
queue . clear ( ) ;
for ( QMap < mtpRequestId , QByteArray > : : const_iterator i = requestsSent . cbegin ( ) , e = requestsSent . cend ( ) ; i ! = e ; + + i ) {
MTP : : cancel ( i . key ( ) ) ;
}
requestsSent . clear ( ) ;
for ( QMap < mtpRequestId , int32 > : : const_iterator i = docRequestsSent . cbegin ( ) , e = docRequestsSent . cend ( ) ; i ! = e ; + + i ) {
MTP : : cancel ( i . key ( ) ) ;
}
docRequestsSent . clear ( ) ;
2014-10-30 16:23:44 +00:00
dcMap . clear ( ) ;
2014-05-30 08:53:19 +00:00
sentSize = 0 ;
2014-10-30 16:23:44 +00:00
for ( int32 i = 0 ; i < MTPUploadSessionsCount ; + + i ) {
2014-12-05 13:44:27 +00:00
MTP : : stopSession ( MTP : : upl [ i ] ) ;
2014-10-30 16:23:44 +00:00
sentSizes [ i ] = 0 ;
}
killSessionsTimer . stop ( ) ;
2014-05-30 08:53:19 +00:00
}
void FileUploader : : partLoaded ( const MTPBool & result , mtpRequestId requestId ) {
QMap < mtpRequestId , int32 > : : iterator j = docRequestsSent . end ( ) ;
QMap < mtpRequestId , QByteArray > : : iterator i = requestsSent . find ( requestId ) ;
if ( i = = requestsSent . cend ( ) ) {
j = docRequestsSent . find ( requestId ) ;
}
if ( i ! = requestsSent . cend ( ) | | j ! = docRequestsSent . cend ( ) ) {
2015-10-29 00:16:52 +00:00
if ( mtpIsFalse ( result ) ) { // failed to upload current file
2014-05-30 08:53:19 +00:00
currentFailed ( ) ;
return ;
} else {
2014-10-30 16:23:44 +00:00
QMap < mtpRequestId , int32 > : : iterator dcIt = dcMap . find ( requestId ) ;
if ( dcIt = = dcMap . cend ( ) ) { // must not happen
currentFailed ( ) ;
return ;
}
int32 dc = dcIt . value ( ) ;
dcMap . erase ( dcIt ) ;
2015-12-24 19:26:28 +00:00
int32 sentPartSize = 0 ;
2014-05-30 08:53:19 +00:00
Queue : : const_iterator k = queue . constFind ( uploading ) ;
if ( i ! = requestsSent . cend ( ) ) {
2015-12-24 19:26:28 +00:00
sentPartSize = i . value ( ) . size ( ) ;
2014-05-30 08:53:19 +00:00
requestsSent . erase ( i ) ;
} else {
2015-12-24 19:26:28 +00:00
sentPartSize = k - > docPartSize ;
2014-05-30 08:53:19 +00:00
docRequestsSent . erase ( j ) ;
}
2015-12-24 19:26:28 +00:00
sentSize - = sentPartSize ;
sentSizes [ dc ] - = sentPartSize ;
2015-10-27 02:39:02 +00:00
if ( k - > type ( ) = = PreparePhoto ) {
2015-12-24 19:26:28 +00:00
k - > fileSentSize + = sentPartSize ;
PhotoData * photo = App : : photo ( k - > id ( ) ) ;
if ( photo - > uploading ( ) & & k - > file ) {
photo - > uploadingData - > size = k - > file - > partssize ;
photo - > uploadingData - > offset = k - > fileSentSize ;
}
2014-05-30 08:53:19 +00:00
emit photoProgress ( k . key ( ) ) ;
2016-02-12 16:35:06 +00:00
} else if ( k - > type ( ) = = PrepareDocument | | k - > type ( ) = = PrepareAudio ) {
2015-10-27 02:39:02 +00:00
DocumentData * doc = App : : document ( k - > id ( ) ) ;
2015-12-24 19:26:28 +00:00
if ( doc - > uploading ( ) ) {
2014-05-30 08:53:19 +00:00
doc - > uploadOffset = ( k - > docSentParts - docRequestsSent . size ( ) ) * k - > docPartSize ;
if ( doc - > uploadOffset > doc - > size ) {
doc - > uploadOffset = doc - > size ;
}
}
emit documentProgress ( k . key ( ) ) ;
}
}
}
sendNext ( ) ;
}
2015-04-04 20:01:34 +00:00
bool FileUploader : : partFailed ( const RPCError & error , mtpRequestId requestId ) {
2015-10-06 19:49:23 +00:00
if ( mtpIsFlood ( error ) ) return false ;
2015-04-04 20:01:34 +00:00
2014-05-30 08:53:19 +00:00
if ( requestsSent . constFind ( requestId ) ! = requestsSent . cend ( ) | | docRequestsSent . constFind ( requestId ) ! = docRequestsSent . cend ( ) ) { // failed to upload current file
currentFailed ( ) ;
}
sendNext ( ) ;
return true ;
}