1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/**
* @file examples/simple_client/simple_client.cpp
* @brief Example app
*
* (c) 2013-2025 by Mega Limited, Auckland, New Zealand
*
* This file is part of the MEGA SDK - Client Access Engine.
*
* Applications using the MEGA API must present a valid application key
* and comply with the the rules set forth in the Terms of Service.
*
* The MEGA SDK 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.
*
* @copyright Simplified (2-clause) BSD License.
*
* You should have received a copy of the license along with this
* program.
*/
#include <chrono>
#include <iostream>
#include <megaapi.h>
#include <thread>
#include <time.h>
// ENTER YOUR CREDENTIALS HERE
#define MEGA_EMAIL "EMAIL"
#define MEGA_PASSWORD "PASSWORD"
// Get yours for free at https://mega.io/developers#source-code
#define APP_KEY "9gETCbhB"
#define USER_AGENT "Simple-Client example app"
using namespace mega;
class MyListener: public MegaListener
{
public:
bool finished{};
virtual void onRequestFinish(MegaApi* api, MegaRequest* request, MegaError* e)
{
if (e->getErrorCode() != MegaError::API_OK)
{
finished = true;
return;
}
switch (request->getType())
{
case MegaRequest::TYPE_LOGIN:
{
api->fetchNodes();
break;
}
case MegaRequest::TYPE_FETCH_NODES:
{
std::cout << "***** Showing files/folders in the root folder:" << std::endl;
MegaNode* root = api->getRootNode();
MegaNodeList* list = api->getChildren(root);
for (int i = 0; i < list->size(); i++)
{
MegaNode* node = list->get(i);
if (node->isFile())
std::cout << "***** File: ";
else
std::cout << "***** Folder: ";
std::cout << node->getName() << std::endl;
}
std::cout << "***** Done" << std::endl;
delete list;
std::cout << "***** Uploading the image MEGA.png" << std::endl;
MegaUploadOptions uploadOptions;
uploadOptions.mtime = 0;
api->startUpload(std::string{"MEGA.png"}, root, nullptr, &uploadOptions, nullptr);
delete root;
break;
}
default:
break;
}
}
// Currently, this callback is only valid for the request fetchNodes()
virtual void onRequestUpdate(MegaApi*, MegaRequest* request)
{
std::cout << "***** Loading filesystem " << request->getTransferredBytes() << " / "
<< request->getTotalBytes() << std::endl;
}
virtual void onRequestTemporaryError(MegaApi*, MegaRequest*, MegaError* error)
{
std::cout << "***** Temporary error in request: " << error->getErrorString() << std::endl;
}
virtual void onTransferFinish(MegaApi*, MegaTransfer*, MegaError* error)
{
if (error->getErrorCode())
{
std::cout << "***** Transfer finished with error: " << error->getErrorString()
<< std::endl;
}
else
{
std::cout << "***** Transfer finished OK" << std::endl;
}
finished = true;
}
virtual void onTransferUpdate(MegaApi*, MegaTransfer* transfer)
{
std::cout << "***** Transfer progress: " << transfer->getTransferredBytes() << "/"
<< transfer->getTotalBytes() << std::endl;
}
virtual void onTransferTemporaryError(MegaApi*, MegaTransfer*, MegaError* error)
{
std::cout << "***** Temporary error in transfer: " << error->getErrorString() << std::endl;
}
virtual void onUsersUpdate(MegaApi*, MegaUserList* users)
{
if (users == NULL)
{
// Full account reload
return;
}
std::cout << "***** There are " << users->size() << " new or updated users in your account"
<< std::endl;
}
virtual void onNodesUpdate(MegaApi*, MegaNodeList* nodes)
{
if (nodes == NULL)
{
// Full account reload
return;
}
std::cout << "***** There are " << nodes->size() << " new or updated node/s in your account"
<< std::endl;
}
virtual void onSetsUpdate(MegaApi*, MegaSetList* sets)
{
if (sets)
{
std::cout << "***** There are " << sets->size()
<< " new or updated Set/s in your account" << std::endl;
}
}
virtual void onSetElementsUpdate(MegaApi*, MegaSetElementList* elements)
{
if (elements)
{
std::cout << "***** There are " << elements->size()
<< " new or updated Set-Element/s in your account" << std::endl;
}
}
};
std::string displayTime(time_t t)
{
char timebuf[32];
strftime(timebuf, sizeof timebuf, "%c", localtime(&t));
return timebuf;
}
int main()
{
// Check the documentation of MegaApi to know how to enable local caching
MegaApi* megaApi = new MegaApi(APP_KEY, ".", USER_AGENT);
// By default, logs are sent to stdout
// You can use MegaApi::setLoggerObject to receive SDK logs in your app
megaApi->setLogLevel(MegaApi::LOG_LEVEL_INFO);
MyListener listener;
// Listener to receive information about all request and transfers
// It is also possible to register a different listener per request/transfer
megaApi->addListener(&listener);
if (std::string{MEGA_EMAIL} == "EMAIL")
{
std::cout << "Please enter your email/password at the top of simple_client.cpp"
<< std::endl;
std::cout << "Press Enter to exit the app..." << std::endl;
getchar();
return 0;
}
// Login. You can get the result in the onRequestFinish callback of your listener
megaApi->login(MEGA_EMAIL, MEGA_PASSWORD);
// You can use the main thread to show a GUI or anything else. MegaApi runs in a background
// thread.
while (!listener.finished)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
// Add code here to exercise MegaApi
#ifdef HAVE_LIBUV
std::cout << "Do you want to enable the local HTTP server (y/n)?" << std::endl;
int c = getchar();
if (c == 'y' || c == 'Y')
{
megaApi->httpServerStart();
megaApi->httpServerSetRestrictedMode(MegaApi::HTTP_SERVER_ALLOW_ALL);
megaApi->httpServerEnableFileServer(true);
megaApi->httpServerEnableFolderServer(true);
std::cout << "You can browse your account now! http://127.0.0.1:4443/" << std::endl;
}
#endif
std::cout << "Press Enter to exit the app..." << std::endl;
getchar();
return 0;
}
|