Invalid multipart request with 0 mime parts google drive upload - qt

I am trying to upload image to google-drive in qt/c++.
My code:
void googled::newuploadSettings(QNetworkReply *reply){
QByteArray m_boundary;
m_boundary = "--";
m_boundary += QString("42 + 13").toAscii();
QByteArray data = reply->readAll();
qDebug() << data;
QString x = getValue(data,"access_token");
qDebug() << x;
x = "Bearer " + x;
qDebug() << x;
QNetworkRequest request;
QUrl url("https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart");
request.setUrl(url);
request.setRawHeader("Content-Length","200000000");
QString y = "multipart/related; boundary=" + QString("42+13");
qDebug() << y;
request.setRawHeader("Content-Type",y.toAscii());
request.setRawHeader("Authorization",x.toLatin1());
QString str;
str += m_boundary;
str += "\r\n";
str += "Content-Disposition: form-data; title=\"";
str += QString("sp").toAscii();
str += "\"; ";
str += "filename=\"";
str += QFile::encodeName("kashmir");
str += "\"\r\n";
str += "Content-Length: " ;
str += QString("200000000").toAscii();
str += "\r\n";
str += "Content-Type: ";
str += QString("application/json; charset=UTF-8").toAscii();
str += "\r\n";
str += "Mime-version: 1.0 ";
str += "\r\n";
str += "\r\n";
str += "mimeType:image/jpeg";
str += "\r\n";
str += "\r\n\r\n";
str += m_boundary;
str += "Content-Type: ";
str += "image/jpeg";
QByteArray arr;
arr.append(str.toUtf8());
QFile file("/home/saurabh/Pictures/005.jpg");
file.open(QIODevice::ReadOnly);
arr.append(file.readAll());
arr.append(m_boundary);
file.close();
qDebug() << "file";
//qDebug() << str;
qDebug() << arr;
m_netM = new QNetworkAccessManager;
QObject::connect(m_netM, SIGNAL(finished(QNetworkReply *)),
this, SLOT(uploadfinishedSlot(QNetworkReply *)));
m_netM->post(request,arr);
}

You are not using the quotation marks for the boundary attribute. Use the following content-type:
QString y = "multipart/related; boundary=\"" + QString("42+13") + "\"";

Related

How to Convert Filetime to Datetime in C++ (MFC/Win32)

I am trying to get Windows boot up time using WMI query and got it as CIM_DATETIME format.
I converted it into File time .The value I get form it is 132372033265000000.
I need to convert it in to Date time(Sunday, June 21, 2020 8:55:27am).
I found many solutions in C# but couldn't able to find one in C++.
Here is a solution that takes the current locale into account. I takes a SYSTEMTIME but you can easily convert that from a FILETIME:
CString DateTimeStr(const SYSTEMTIME *pST, bool bSeconds, bool bDate, bool bTime, bool bLongDate, bool bMilliSec, LCID Locale)
{
CString sDate, sTime;
DWORD dwFlags;
int iStrLen;
LPTSTR pBuf;
if (bDate)
{
dwFlags = 0;
if (bLongDate)
dwFlags |= DATE_LONGDATE;
else
dwFlags |= DATE_SHORTDATE;
iStrLen = GetDateFormat(Locale, dwFlags, pST, NULL, NULL, 0);
pBuf = sDate.GetBuffer(iStrLen + 2);
(void)GetDateFormat(Locale, dwFlags, pST, NULL, pBuf, iStrLen+2);
sDate.ReleaseBuffer();
}
if (bTime)
{
dwFlags = 0;
if (!bSeconds)
dwFlags |= TIME_NOSECONDS;
iStrLen = GetTimeFormat(Locale, dwFlags, pST, NULL, NULL, 0);
pBuf = sTime.GetBuffer(iStrLen + 2);
(void)GetTimeFormat(Locale, dwFlags, pST, NULL, pBuf, iStrLen+2);
sTime.ReleaseBuffer();
if (bMilliSec)
sTime += _T(" ") + FmtNum(pST->wMilliseconds, 3, true) + TEXT(" ms");
}
if (bDate && bTime)
return sDate + _T(" ") + sTime;
if (bDate)
return sDate;
if (bTime)
return sTime;
return (_T(""));
}
To convert a FILETIME structure into a time that is easy to
display to a user, use the FileTimeToSystemTime function.
Code Sample:
#include <Windows.h>
#include <iostream>
int main()
{
const char *day[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
const char* month[] = {"January","February","March","April","May","June","July", "August","September","October","November","December"};
long long value = 132372033265000000;
FILETIME ft = { 0 };
ft.dwHighDateTime = (value & 0xffffffff00000000) >> 32;
ft.dwLowDateTime = value & 0xffffffff;
SYSTEMTIME sys = { 0 };
FileTimeToSystemTime(&ft, &sys);
std::cout << day[sys.wDayOfWeek] << "," << month[sys.wMonth] << " " << sys.wDay << "," << sys.wYear << " "<< sys.wHour << ":" << sys.wMinute << ":" << sys.wSecond;
return 0;
}
Output:
Sunday,July 21,2020 8:55:26

RSA encryption/decryption method

i've seen this interesting method to encrypt/decrypt messages with RSA on YouTube and it works, I've tested it : https://www.youtube.com/watch?v=tXXnHXslVhw&t=98s minute 1:45 . I am not that good at math, is there a name for what this guy is doing?
void En() {
crypted= text;
unsigned long long temp=0;
unsigned long long enc = 0;
for (int i = 0; i < text.length() / 2; i++)
{
if (text[i]>='a' && text[i] <= 'z')
{
temp = (text[i] - 96) * 26 + text[i + 1] - 96;
enc = pow(temp, public_key);
enc= enc % N
cout << enc << endl;
enc_v2 = enc;
}
}
cout << "Enc: " << enc << endl;}
void De() {
unsigned long long temp2 = 0;
unsigned long long temp = 0;
char ch=' ', ch2=' ';
for (int i = 0; i < text.length()/2; i++)
{
cout << enc_v2 << private_key;
temp = pow(enc_v2, private_key);
cout << "Temp :" << temp;
temp = temp % N;
cout << "Temp modulo :" << temp;
temp2 = temp;
temp = temp / 26;
cout << " Temp char 1 :"<< temp;
ch = temp + 96;
temp2 = temp2 - temp * 26;
cout << " Temp char 1 :" << temp2;
ch2 = temp2 + 96;
}
cout << "Text: " << ch << ch2;}
P.S. I know about the pow and modular exponentiation , this in only to show what he is doing.
Thank you!

Sending Floats from server to client through socket (UDP)

So I am trying to send floats back and forth between clients and a server. I am able to send floats to the server and the server reads them perfectly. But unfortunately I can't seem to get the server sending the floats back to the other clients. Our current code that doesn't work looks like this.
Send function on server side
int Server::Send(float dt) {
{
char message[BUFLEN];
std::string msg = std::to_string(x2) + "#" + std::to_string(z2);
strcpy(message, (char*)msg.c_str());
if (sendto(server_socket, message, sizeof(message), 0, ptr->ai_addr, ptr->ai_addrlen) == SOCKET_ERROR)
{
std::cout << "Sendto() failed..." << std::endl;
}
else
{
std::cout << "Sent: " << message << "\n" << std::endl;
}
Receiving on Client side
int Client::Recieve(float dt) {
char buf[BUFLEN];
struct sockaddr_in fromAddr;
int fromlen;
fromlen = sizeof(fromAddr);
memset(buf, 0, BUFLEN);
int bytes_recieved = -1;
int sError = -1;
bytes_recieved = recvfrom(client_socket, buf, BUFLEN, 0, (struct sockaddr*) &fromAddr, &fromlen);
sError = WSAGetLastError();
if (sError != WSAEWOULDBLOCK && bytes_recieved > 0)
{
std::cout << "Recieved: " << buf << std::endl;
std::string tmp = buf;
std::size_t pos = tmp.find("#");
tmp = tmp.substr(0, pos - 1);
x2 = std::stof(tmp, NULL);
tmp = buf;
tmp = tmp.substr(pos + 1);
z2 = std::stof(tmp, NULL);
std::cout << "tx: " << x2 << " ty: " << z2 << std::endl;
}
else
std::cout << "Not Receiving" << std::endl;
Any help would be awesome!

How to show a floating number with a fixed amount of digits in Qt?

I want to display a floating number in Qt with a fixed amount of digits (4), but without filling non-used digits with zero (the equivalent of having a maximum number of digits). In other words, this is what I want to show for the following examples:
0 -> 0.0
10 -> 10.0
980.5 -> 980.5
1200.5 -> 1.200 k
9900.9 -> 9.900 k
120500.9 -> 120.5 k
999888.88 -> 999.9 k
etc.. I tried many combinations of both QString::number() as well as QString::args(), without success. So how can I do that?
Note: I'm aware that for numbers higher then 1000, I'll have to apply a division and add the label 'k' manually - I'm already doing that.
EDIT:
The following code does exactly what I want, only that it is quite inappropriate with all those if else. I would like to know how can I do that with Qt's functions:
float temp = getSomeValue();
const char* itemUnities[] = { "V", "W", "A", "J" };
if (temp < 10.0f)
{
painter.drawText(defaultX + 60,yPosition,QString::number(temp,'f',3));
painter.drawText(defaultX + 110,yPosition,tr(itemUnities[aaa]));
}
else if (temp < 100.0f)
{
painter.drawText(defaultX + 60,yPosition,QString::number(temp,'f',2));
painter.drawText(defaultX + 110,yPosition,tr(itemUnities[aaa]));
}
else if (temp < 1000.0f)
{
painter.drawText(defaultX + 60,yPosition,QString::number(temp,'f',1));
painter.drawText(defaultX + 110,yPosition,tr(itemUnities[aaa]));
}
else if (temp < 10000.0f)
{
temp *= 0.001;
painter.drawText(defaultX + 60,yPosition,QString::number(temp,'f',3));
painter.drawText(defaultX + 110,yPosition,tr("k") + tr(itemUnities[aaa]));
}
else if (temp < 100000.0f)
{
temp *= 0.001;
painter.drawText(defaultX + 60,yPosition,QString::number(temp,'f',2));
painter.drawText(defaultX + 110,yPosition,tr("k") + tr(itemUnities[aaa]));
}
else if (temp < 1000000.0f)
{
temp *= 0.001;
painter.drawText(defaultX + 60,yPosition,QString::number(temp,'f',1));
painter.drawText(defaultX + 110,yPosition,tr("k") + tr(itemUnities[aaa]));
}
else if (temp < 10000000.0f)
{
temp *= 0.000001;
painter.drawText(defaultX + 60,yPosition,QString::number(temp,'f',3));
painter.drawText(defaultX + 110,yPosition,tr("M") + tr(itemUnities[aaa]));
}
else if (temp < 100000000.0f)
{
temp *= 0.000001;
painter.drawText(defaultX + 60,yPosition,QString::number(temp,'f',2));
painter.drawText(defaultX + 110,yPosition,tr("M") + tr(itemUnities[aaa]));
}
else if (temp < 1000000000.0f)
{
temp *= 0.000001;
painter.drawText(defaultX + 60,yPosition,QString::number(temp,'f',1));
painter.drawText(defaultX + 110,yPosition,tr("M") + tr(itemUnities[aaa]));
}
Since you have very specific wishes for the conversion, a built in method does not provide all the functionality, but with some tricks, you can use QString::number() and QString::truncate():
QString doubleToQStr(const double val, const size_t d)
{
QString str = QString::number(val,'g',15);
if( val >= std::pow(10.0,static_cast<double>(d-1)) )
{
str.truncate(d);
// find magnitude
size_t mag = 0;
while( val >= std::pow( 10.0, static_cast<double>(mag) ) ) { mag++; }
if ( mag > 3 )
{
size_t dotpos = mag % 3;
str.insert(dotpos,".");
size_t mag3 = mag - dotpos;
switch( mag3 )
{
case 3:
str += " k"; break;
case 6:
str += " M"; break;
case 9:
str += " G"; break;
case 12:
str += " T"; break;
default:
str += " ?"; break;
}
}
}
else if ( val < std::pow(10.0,static_cast<double>(-(static_cast<int>(d)-1))) )
{ str = "0.0"; }
else
{ str.truncate(d+1); }
return str;
}
Here are the test cases I used:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
std::cout << "99919999.9 -> " << doubleToQStr(99919999.9, 4).toStdString() << std::endl;
std::cout << "9999.9 -> " << doubleToQStr(9999.9, 4).toStdString() << std::endl;
std::cout << "999.9 -> " << doubleToQStr(999.9, 4).toStdString() << std::endl;
std::cout << "99.9 -> " << doubleToQStr(99.9, 4).toStdString() << std::endl;
std::cout << "9.9 -> " << doubleToQStr(9.9, 4).toStdString() << std::endl;
std::cout << "0.9 -> " << doubleToQStr(0.9, 4).toStdString() << std::endl;
std::cout << "0.09 -> " << doubleToQStr(0.09, 4).toStdString() << std::endl;
std::cout << "0.009 -> " << doubleToQStr(0.009, 4).toStdString() << std::endl;
std::cout << "0.0009 -> " << doubleToQStr(0.0009, 4).toStdString() << std::endl;
std::cout << "0.00009 -> " << doubleToQStr(0.00009, 4).toStdString() << std::endl;
return a.exec();
}

Parsing texts between " " on a file to a QString

I have a text file which looks like this:
VariableA = 10 VariableB = 20 VariableC = "Hello World"
The code works fine, but my trouble is getting the text strings between " ".
QStringList Data;
Data << "VariableA = " << "VariableB = " << "VariableC = ";
QStringList Values;
int VariableA;
int VariableB;
QString VariableC;
foreach(const QString &DataToFind, Data) {
QRegExp DataExpression(DataToFind);
int DataStart = DataExpression.indexIn(TextToFind);
if(DataStart >= 0) {
int DataLength = DataExpression.matchedLength();
int ValueSize = 1;
while(TextToFind.at(DataStart + DataLength + ValueSize) != QChar(' ')) {
ValueSize++;
}
QStringRef DataValue(&TextToFind, DataStart + DataLength, ValueSize);
Values += DataValue.toString();
DataStart = DataExpression.indexIn(description, DataStart + DataLength);
} else {
continue;
}
}
VariableA = Values[0].toInt();
VariableB = Values[1].toInt();
VariableC = Values[2];
The issue is that the text on VariableC can have spaces and/or " (double quotes) inside it. So the method I've posted above to retrieve the variables from the file is useless. Since it uses " " to reach end of variable in the file.
How can I retrieve the full text inside the double quotes?
QStringList Data;
Data << "A = " << "B = " << "X = ";
int A;
int B;
QString X;
foreach(const QString &DataToFind, Data) {
QRegExp DataExpression(DataToFind);
int DataStart = DataExpression.indexIn(TextToFind);
if(DataStart >= 0) {
int DataLength = DataExpression.matchedLength();
int ValueSize = 1;
while(TextToFind.at(DataStart + DataLength + ValueSize) != QChar(' ')) {
ValueSize++;
}
QStringRef DataValue(&TextToFind, DataStart + DataLength, ValueSize);
DataStart = DataExpression.indexIn(description, DataStart + DataLength);
} else {
continue;
}
}
This does the work.

Resources