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()) {
		if (!result.v) { // failed to upload current file
			currentFailed();
			return;
		} else {
			QMap<mtpRequestId, int32>::iterator dcIt = dcMap.find(requestId);
			if (dcIt == dcMap.cend()) { // must not happen
				currentFailed();
				return;
			}
			int32 dc = dcIt.value();
			dcMap.erase(dcIt);

			Queue::const_iterator k = queue.constFind(uploading);
			if (i != requestsSent.cend()) {
				sentSize -= i.value().size();
				sentSizes[dc] -= i.value().size();
				requestsSent.erase(i);
			} else {
				sentSize -= k->docPartSize;
				sentSizes[dc] -= k->docPartSize;
				docRequestsSent.erase(j);
			}
			if (k->media.type == ToPreparePhoto) {
				emit photoProgress(k.key());
			} else if (k->media.type == ToPrepareDocument) {
				DocumentData *doc = App::document(k->media.id);
				if (doc->status == FileUploading) {
					doc->uploadOffset = (k->docSentParts - docRequestsSent.size()) * k->docPartSize;
					if (doc->uploadOffset > doc->size) {
						doc->uploadOffset = doc->size;
					}
				}
				emit documentProgress(k.key());
			} else if (k->media.type == ToPrepareAudio) {
				AudioData *audio = App::audio(k->media.id);
				if (audio->status == FileUploading) {
					audio->uploadOffset = (k->docSentParts - docRequestsSent.size()) * k->docPartSize;
					if (audio->uploadOffset > audio->size) {
						audio->uploadOffset = audio->size;
					}
				}
			}
		}
	}

	sendNext();
}
Exemple #2
0
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()) {
        if (mtpIsFalse(result)) { // failed to upload current file
            currentFailed();
            return;
        } else {
            QMap<mtpRequestId, int32>::iterator dcIt = dcMap.find(requestId);
            if (dcIt == dcMap.cend()) { // must not happen
                currentFailed();
                return;
            }
            int32 dc = dcIt.value();
            dcMap.erase(dcIt);

            int32 sentPartSize = 0;
            Queue::const_iterator k = queue.constFind(uploading);
            if (i != requestsSent.cend()) {
                sentPartSize = i.value().size();
                requestsSent.erase(i);
            } else {
                sentPartSize = k->docPartSize;
                docRequestsSent.erase(j);
            }
            sentSize -= sentPartSize;
            sentSizes[dc] -= sentPartSize;
            if (k->type() == PreparePhoto) {
                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;
                }
                emit photoProgress(k.key());
            } else if (k->type() == PrepareDocument || k->type() == PrepareAudio) {
                DocumentData *doc = App::document(k->id());
                if (doc->uploading()) {
                    doc->uploadOffset = (k->docSentParts - docRequestsSent.size()) * k->docPartSize;
                    if (doc->uploadOffset > doc->size) {
                        doc->uploadOffset = doc->size;
                    }
                }
                emit documentProgress(k.key());
            }
        }
    }

    sendNext();
}
bool FileUploader::partFailed(const RPCError &err, mtpRequestId requestId) {
	if (requestsSent.constFind(requestId) != requestsSent.cend() || docRequestsSent.constFind(requestId) != docRequestsSent.cend()) { // failed to upload current file
		currentFailed();
	}
	sendNext();
	return true;
}
void FileUploader::cancel(MsgId msgId) {
	uploaded.remove(msgId);
	if (uploading == msgId) {
		currentFailed();
	} else {
		queue.remove(msgId);
	}
}
bool FileUploader::partFailed(const RPCError &error, mtpRequestId requestId) {
	if (error.type().startsWith(qsl("FLOOD_WAIT_"))) return false;

	if (requestsSent.constFind(requestId) != requestsSent.cend() || docRequestsSent.constFind(requestId) != docRequestsSent.cend()) { // failed to upload current file
		currentFailed();
	}
	sendNext();
	return true;
}
Exemple #6
0
bool FileUploader::partFailed(const RPCError &error, mtpRequestId requestId) {
    if (MTP::isDefaultHandledError(error)) return false;

    if (requestsSent.constFind(requestId) != requestsSent.cend() || docRequestsSent.constFind(requestId) != docRequestsSent.cend()) { // failed to upload current file
        currentFailed();
    }
    sendNext();
    return true;
}
void FileUploader::sendNext() {
	if (sentSize >= MaxUploadFileParallelSize) return;

	bool killing = killSessionsTimer.isActive();
	if (queue.isEmpty()) {
		if (!killing) {
			killSessionsTimer.start(MTPAckSendWaiting + MTPKillFileSessionTimeout);
		}
		return;
	}

	if (killing) {
		killSessionsTimer.stop();
	}
	Queue::iterator i = uploading ? queue.find(uploading) : queue.begin();
	if (!uploading) {
		uploading = i.key();
	} else if (i == queue.end()) {
		i = queue.begin(); 
		uploading = i.key();
	}
	int todc = 0;
	for (int dc = 1; dc < MTPUploadSessionsCount; ++dc) {
		if (sentSizes[dc] < sentSizes[todc]) {
			todc = dc;
		}
	}
	if (i->media.parts.isEmpty()) {
		if (i->docSentParts >= i->docPartsCount) {
			if (requestsSent.isEmpty() && docRequestsSent.isEmpty()) {
				if (i->media.type == ToPreparePhoto) {
					emit photoReady(uploading, MTP_inputFile(MTP_long(i->media.id), MTP_int(i->partsCount), MTP_string(i->media.filename), MTP_string(i->media.jpeg_md5)));
				} else if (i->media.type == ToPrepareDocument) {
					QByteArray docMd5(32, Qt::Uninitialized);
					hashMd5Hex(i->docHash.result(), docMd5.data());

					MTPInputFile doc = (i->docSize > UseBigFilesFrom) ? MTP_inputFileBig(MTP_long(i->media.id), MTP_int(i->docPartsCount), MTP_string(i->media.filename)) : MTP_inputFile(MTP_long(i->media.id), MTP_int(i->docPartsCount), MTP_string(i->media.filename), MTP_string(docMd5));
					if (i->partsCount) {
						emit thumbDocumentReady(uploading, doc, MTP_inputFile(MTP_long(i->media.thumbId), MTP_int(i->partsCount), MTP_string(qsl("thumb.") + i->media.thumbExt), MTP_string(i->media.jpeg_md5)));
					} else {
						emit documentReady(uploading, doc);
					}
				}
				queue.remove(uploading);
				uploading = 0;
				sendNext();
			}
			return;
		}

		QByteArray toSend;
		if (i->media.data.isEmpty()) {
			if (!i->docFile) {
				i->docFile.reset(new QFile(i->media.file));
				if (!i->docFile->open(QIODevice::ReadOnly)) {
					currentFailed();
					return;
				}
			}
			toSend = i->docFile->read(i->docPartSize);
			if (i->docSize <= UseBigFilesFrom) {
				i->docHash.feed(toSend.constData(), toSend.size());
			}
		} else {
			toSend = i->media.data.mid(i->docSentParts * i->docPartSize, i->docPartSize);
			if (i->media.type == ToPrepareDocument && i->docSentParts <= UseBigFilesFrom) {
				i->docHash.feed(toSend.constData(), toSend.size());
			}
		}
		if (toSend.size() > i->docPartSize || (toSend.size() < i->docPartSize && i->docSentParts + 1 != i->docPartsCount)) {
			currentFailed();
			return;
		}
		mtpRequestId requestId;
		if (i->docSize > UseBigFilesFrom) {
			requestId = MTP::send(MTPupload_SaveBigFilePart(MTP_long(i->media.id), MTP_int(i->docSentParts), MTP_int(i->docPartsCount), MTP_string(toSend)), rpcDone(&FileUploader::partLoaded), rpcFail(&FileUploader::partFailed), MTP::upl[todc]);
		} else {
			requestId = MTP::send(MTPupload_SaveFilePart(MTP_long(i->media.id), MTP_int(i->docSentParts), MTP_string(toSend)), rpcDone(&FileUploader::partLoaded), rpcFail(&FileUploader::partFailed), MTP::upl[todc]);
		}
		docRequestsSent.insert(requestId, i->docSentParts);
		dcMap.insert(requestId, todc);
		sentSize += i->docPartSize;
		sentSizes[todc] += i->docPartSize;

		i->docSentParts++;
	} else {
		LocalFileParts::iterator part = i->media.parts.begin();
	
		mtpRequestId requestId = MTP::send(MTPupload_SaveFilePart(MTP_long(i->media.thumbId), MTP_int(part.key()), MTP_string(part.value())), rpcDone(&FileUploader::partLoaded), rpcFail(&FileUploader::partFailed), MTP::upl[todc]);
		requestsSent.insert(requestId, part.value());
		dcMap.insert(requestId, todc);
		sentSize += part.value().size();
		sentSizes[todc] += part.value().size();

		i->media.parts.erase(part);
	}
	nextTimer.start(UploadRequestInterval);
}