Changeset View
Standalone View
src/kio_gdrive.cpp
Show First 20 Lines • Show All 45 Lines • ▼ Show 20 Line(s) | |||||
46 | #include <KGAPI/Drive/ParentReference> | 46 | #include <KGAPI/Drive/ParentReference> | ||
47 | #include <KGAPI/Drive/Permission> | 47 | #include <KGAPI/Drive/Permission> | ||
48 | #include <KIO/AccessManager> | 48 | #include <KIO/AccessManager> | ||
49 | #include <KIO/Job> | 49 | #include <KIO/Job> | ||
50 | #include <KLocalizedString> | 50 | #include <KLocalizedString> | ||
51 | 51 | | |||
52 | #include <QNetworkRequest> | 52 | #include <QNetworkRequest> | ||
53 | #include <QNetworkReply> | 53 | #include <QNetworkReply> | ||
54 | #include <QMimeDatabase> | ||||
54 | 55 | | |||
55 | using namespace KGAPI2; | 56 | using namespace KGAPI2; | ||
56 | using namespace Drive; | 57 | using namespace Drive; | ||
57 | 58 | | |||
58 | class KIOPluginForMetaData : public QObject | 59 | class KIOPluginForMetaData : public QObject | ||
59 | { | 60 | { | ||
60 | Q_OBJECT | 61 | Q_OBJECT | ||
61 | Q_PLUGIN_METADATA(IID "org.kde.kio.slave.gdrive" FILE "gdrive.json") | 62 | Q_PLUGIN_METADATA(IID "org.kde.kio.slave.gdrive" FILE "gdrive.json") | ||
▲ Show 20 Lines • Show All 596 Lines • ▼ Show 20 Line(s) | 597 | { | |||
658 | do { | 659 | do { | ||
659 | const size_t nextChunk = qMin(contentData.size() - transferred, 14 * 1024 * 1024); | 660 | const size_t nextChunk = qMin(contentData.size() - transferred, 14 * 1024 * 1024); | ||
660 | data(QByteArray::fromRawData(contentData.constData() + transferred, nextChunk)); | 661 | data(QByteArray::fromRawData(contentData.constData() + transferred, nextChunk)); | ||
661 | transferred += nextChunk; | 662 | transferred += nextChunk; | ||
662 | } while (transferred < contentData.size()); | 663 | } while (transferred < contentData.size()); | ||
663 | finished(); | 664 | finished(); | ||
664 | } | 665 | } | ||
665 | 666 | | |||
666 | bool KIOGDrive::readPutData(QTemporaryFile &tempFile) | 667 | bool KIOGDrive::readPutData(QTemporaryFile &tempFile, FilePtr &fileMetaData) | ||
667 | { | 668 | { | ||
668 | // TODO: Instead of using a temp file, upload directly the raw data (requires | 669 | // TODO: Instead of using a temp file, upload directly the raw data (requires | ||
669 | // support in LibKGAPI) | 670 | // support in LibKGAPI) | ||
670 | 671 | | |||
671 | // TODO: For large files, switch to resumable upload and upload the file in | 672 | // TODO: For large files, switch to resumable upload and upload the file in | ||
672 | // reasonably large chunks (requires support in LibKGAPI) | 673 | // reasonably large chunks (requires support in LibKGAPI) | ||
673 | 674 | | |||
674 | // TODO: Support resumable upload (requires support in LibKGAPI) | 675 | // TODO: Support resumable upload (requires support in LibKGAPI) | ||
Show All 11 Lines | 683 | do { | |||
686 | if (!buffer.isEmpty()) { | 687 | if (!buffer.isEmpty()) { | ||
687 | qint64 size = tempFile.write(buffer); | 688 | qint64 size = tempFile.write(buffer); | ||
688 | if (size != buffer.size()) { | 689 | if (size != buffer.size()) { | ||
689 | error(KIO::ERR_CANNOT_WRITE, tempFile.fileName()); | 690 | error(KIO::ERR_CANNOT_WRITE, tempFile.fileName()); | ||
690 | return false; | 691 | return false; | ||
691 | } | 692 | } | ||
692 | } | 693 | } | ||
693 | } while (result > 0); | 694 | } while (result > 0); | ||
695 | | ||||
696 | const QMimeType mime = QMimeDatabase().mimeTypeForFileNameAndData(fileMetaData->title(), &tempFile); | ||||
697 | fileMetaData->setMimeType(mime.name()); | ||||
elvisangelaccio: I'd just use `QMimeDatabase().mimeTypeForFileNameAndData(f...)` | |||||
698 | | ||||
694 | tempFile.close(); | 699 | tempFile.close(); | ||
695 | 700 | | |||
696 | if (result == -1) { | 701 | if (result == -1) { | ||
697 | qCWarning(GDRIVE) << "Could not read source file" << tempFile.fileName(); | 702 | qCWarning(GDRIVE) << "Could not read source file" << tempFile.fileName(); | ||
698 | error(KIO::ERR_CANNOT_READ, QString()); | 703 | error(KIO::ERR_CANNOT_READ, QString()); | ||
699 | return false; | 704 | return false; | ||
700 | } | 705 | } | ||
701 | 706 | | |||
Show All 36 Lines | 733 | { | |||
738 | } | 743 | } | ||
739 | 744 | | |||
740 | const ObjectsList objects = fetchJob.items(); | 745 | const ObjectsList objects = fetchJob.items(); | ||
741 | if (objects.size() != 1) { | 746 | if (objects.size() != 1) { | ||
742 | putCreate(url); | 747 | putCreate(url); | ||
743 | return false; | 748 | return false; | ||
744 | } | 749 | } | ||
745 | 750 | | |||
746 | const FilePtr file = objects[0].dynamicCast<File>(); | 751 | FilePtr file = objects[0].dynamicCast<File>(); | ||
752 | | ||||
747 | QTemporaryFile tmpFile; | 753 | QTemporaryFile tmpFile; | ||
elvisangelaccio: This comment is now obsolete. I'd just remove it. | |||||
748 | if (!readPutData(tmpFile)) { | 754 | if (!readPutData(tmpFile, file)) { | ||
749 | error(KIO::ERR_CANNOT_READ, url.path()); | 755 | error(KIO::ERR_CANNOT_READ, url.path()); | ||
Doesn't this result in temp files such as /tmp/XXXXXXfoo.txt ? Don't we need a slash between the template and the filename? elvisangelaccio: Doesn't this result in temp files such as `/tmp/XXXXXXfoo.txt` ? Don't we need a slash between… | |||||
Indeed, those would be the resulting temp filenames, as opposed to the current /tmp/XXXXXX. I believe adding a slash would result in a unique folder named XXXXXX (with inside only foo.txt) being created relative to the current working directory (per doc). barchiesi: Indeed, those would be the resulting temp filenames, as opposed to the current `/tmp/XXXXXX`. I… | |||||
Ah right. Can we use a - as separator then? ( /tmp/XXXXXX-foo.txt ) elvisangelaccio: Ah right. Can we use a `-` as separator then? ( `/tmp/XXXXXX-foo.txt` ) | |||||
Maybe the LibKGAPI API should be extended to allow the caller to specify the mime type, because the caller is always more knowledgable about the file than LibKGAPI, and use QMimeDatabase::mimeTypeForFileNameAndData() just as a fallback when caller doesn't provide the mime type. Putting the real filename into /tmp is a potential privacy leak - other users of the system might be able to see what you are uploading. dvratil: Maybe the LibKGAPI API should be extended to allow the caller to specify the mime type, because… | |||||
That indeed seems like a better idea. I'll look into that and try to also tackle the TODO at line 666 of this file. // TODO: Instead of using a temp file, upload directly the raw data (requires // support in LibKGAPI) barchiesi: That indeed seems like a better idea. I'll look into that and try to also tackle the TODO at… | |||||
750 | return false; | 756 | return false; | ||
751 | } | 757 | } | ||
752 | 758 | | |||
753 | FileModifyJob modifyJob(tmpFile.fileName(), file, getAccount(accountId)); | 759 | FileModifyJob modifyJob(tmpFile.fileName(), file, getAccount(accountId)); | ||
754 | modifyJob.setUpdateModifiedDate(true); | 760 | modifyJob.setUpdateModifiedDate(true); | ||
755 | if (!runJob(modifyJob, url, accountId)) { | 761 | if (!runJob(modifyJob, url, accountId)) { | ||
756 | return false; | 762 | return false; | ||
757 | } | 763 | } | ||
Show All 28 Lines | 769 | { | |||
786 | /* | 792 | /* | ||
787 | if (hasMetaData(QLatin1String("modified"))) { | 793 | if (hasMetaData(QLatin1String("modified"))) { | ||
788 | const QString modified = metaData(QLatin1String("modified")); | 794 | const QString modified = metaData(QLatin1String("modified")); | ||
789 | qCDebug(GDRIVE) << modified; | 795 | qCDebug(GDRIVE) << modified; | ||
790 | file->setModifiedDate(KDateTime::fromString(modified, KDateTime::ISODate)); | 796 | file->setModifiedDate(KDateTime::fromString(modified, KDateTime::ISODate)); | ||
791 | } | 797 | } | ||
792 | */ | 798 | */ | ||
793 | 799 | | |||
794 | QTemporaryFile tmpFile; | 800 | QTemporaryFile tmpFile; | ||
elvisangelaccio: Same | |||||
795 | if (!readPutData(tmpFile)) { | 801 | if (!readPutData(tmpFile, file)) { | ||
796 | error(KIO::ERR_CANNOT_READ, url.path()); | 802 | error(KIO::ERR_CANNOT_READ, url.path()); | ||
797 | return false; | 803 | return false; | ||
798 | } | 804 | } | ||
799 | 805 | | |||
800 | const auto accountId = gdriveUrl.account(); | 806 | const auto accountId = gdriveUrl.account(); | ||
801 | FileCreateJob createJob(tmpFile.fileName(), file, getAccount(accountId)); | 807 | FileCreateJob createJob(tmpFile.fileName(), file, getAccount(accountId)); | ||
802 | if (!runJob(createJob, url, accountId)) { | 808 | if (!runJob(createJob, url, accountId)) { | ||
803 | return false; | 809 | return false; | ||
▲ Show 20 Lines • Show All 313 Lines • Show Last 20 Lines |
I'd just use QMimeDatabase().mimeTypeForFileNameAndData(f...)