enable first test of webDAV api class

pull/23/head
JuanJakobo 2022-07-12 18:14:19 +02:00
parent 673e15f164
commit 2a09d8bee3
3 changed files with 86 additions and 126 deletions

View File

@ -27,24 +27,24 @@ WebDAV::WebDAV()
//TODO update on first login only start and create update button, update others just if etag changed //TODO update on first login only start and create update button, update others just if etag changed
//save all to sqlite //save all to sqlite
Log::writeInfoLog(NEXTCLOUD_PATH);
if (iv_access(NEXTCLOUD_PATH.c_str(), W_OK) != 0) if (iv_access(NEXTCLOUD_PATH.c_str(), W_OK) != 0)
iv_mkdir(NEXTCLOUD_PATH.c_str(), 0777); iv_mkdir(NEXTCLOUD_PATH.c_str(), 0777);
Log::writeInfoLog(NEXTCLOUD_FILE_PATH);
if (iv_access(NEXTCLOUD_FILE_PATH.c_str(), W_OK) != 0) if (iv_access(NEXTCLOUD_FILE_PATH.c_str(), W_OK) != 0)
iv_mkdir(NEXTCLOUD_FILE_PATH.c_str(), 0777); iv_mkdir(NEXTCLOUD_FILE_PATH.c_str(), 0777);
Log::writeInfoLog(CONFIG_PATH);
if (iv_access(CONFIG_PATH.c_str(), W_OK) == 0) if (iv_access(CONFIG_PATH.c_str(), W_OK) == 0)
{ {
_username = Util::accessConfig(Action::IReadSecret,"Username"); _username = Util::accessConfig(Action::IReadString,"username");
_password = Util::accessConfig(Action::IReadSecret,"Password"); _password = Util::accessConfig(Action::IReadSecret,"password");
} }
if(_username.empty()) if(_username.empty())
Log::writeErrorLog("username not set"); Log::writeErrorLog("username not set");
//test login //test login
//
//
//create database
} }
//TODO pas as reversne and no return //TODO pas as reversne and no return
@ -57,125 +57,80 @@ string WebDAV::getLocalPath(string path)
return NEXTCLOUD_FILE_PATH + "/" + path; return NEXTCLOUD_FILE_PATH + "/" + path;
} }
//TODO SQL CHeck before calling this function //TODO SQL CHeck before calling this function --> if is needed...
///TODO rename function
vector<WebDAVItem> WebDAV::getDataStructure(const string &pathUrl) vector<WebDAVItem> WebDAV::getDataStructure(const string &pathUrl)
{ {
string xmlItem = propfind(pathUrl); string xmlItem = propfind(pathUrl);
if(!xmlItem.empty())
size_t begin;
size_t end;
string beginItem = "<d:response>";
string endItem = "</d:response>";
vector<WebDAVItem> tempItems;
WebDAVItem tempItem;
begin = xmlItem.find(beginItem);
while (begin != std::string::npos)
{ {
end = xmlItem.find(endItem); string beginItem = "<d:response>";
//tempItems.push_back(xml.substr(begin, end)); string endItem = "</d:response>";
vector<WebDAVItem> tempItems;
WebDAVItem tempItem;
size_t begin = xmlItem.find(beginItem);
size_t end;
tempItem.etag = Util::getXMLAttribute(xmlItem, "d:getetag"); while (begin != std::string::npos)
Log::writeInfoLog("etag " + tempItem.etag);
Util::decodeUrl(tempItem.etag);
Log::writeInfoLog("etag " + tempItem.etag);
//TODO escape quot
tempItem.path = Util::getXMLAttribute(xmlItem, "d:href");
//TODO dont remove?
//replaces everthing in front of /remote.php as this is already part of the url
Log::writeInfoLog("path before transformation " + tempItem.path);
if(tempItem.path.find(NEXTCLOUD_START_PATH) != 0)
{ {
tempItem.path.erase(0,tempItem.path.find(NEXTCLOUD_START_PATH)); end = xmlItem.find(endItem);
Log::writeInfoLog("path after transformation " + tempItem.path); //TODO use xml lib?
//TODO fav is int?
//Log::writeInfoLog(Util::getXMLAttribute(xmlItem, "d:favorite"));
tempItem.etag = Util::getXMLAttribute(xmlItem, "d:getetag");
tempItem.path = Util::getXMLAttribute(xmlItem, "d:href");
tempItem.lastEditDate = Util::getXMLAttribute(xmlItem, "d:getlastmodified");
tempItem.size = atof(Util::getXMLAttribute(xmlItem, "oc:size").c_str());
//replaces everthing in front of /remote.php as this is already part of the url
if(tempItem.path.find(NEXTCLOUD_START_PATH) != 0)
tempItem.path.erase(0,tempItem.path.find(NEXTCLOUD_START_PATH));
tempItem.title = tempItem.path;
tempItem.localPath = getLocalPath(tempItem.path);
if (tempItem.path.back() == '/')
{
tempItem.localPath = tempItem.localPath.substr(0, tempItem.localPath.length() - 1);
tempItem.type = Itemtype::IFOLDER;
tempItem.title = tempItem.title.substr(0, tempItem.path.length() - 1);
//TODO set sync status of folders --> use sqlite?
}
else
{
tempItem.type = Itemtype::IFILE;
tempItem.fileType = Util::getXMLAttribute(xmlItem, "d:getcontenttype");
if (iv_access(tempItem.localPath.c_str(), W_OK) != 0)
tempItem.state = FileState::ICLOUD;
else
tempItem.state = FileState::ISYNCED;
}
tempItem.title = tempItem.title.substr(tempItem.title.find_last_of("/") + 1, tempItem.title.length());
Util::decodeUrl(tempItem.title);
tempItems.push_back(tempItem);
xmlItem = xmlItem.substr(end + endItem.length());
begin = xmlItem.find(beginItem);
} }
tempItem.lastEditDate = Util::getXMLAttribute(xmlItem, "d:getlastmodified");
tempItem.title = tempItem.path; //TODO why twice?
tempItem.localPath = getLocalPath(tempItem.path);
if (tempItem.path.back() == '/') //TODO doppelt
{ if (tempItems.empty())
tempItem.localPath = tempItem.localPath.substr(0, tempItem.localPath.length() - 1); return {};
//tempItem.type = Itemtype::IFOLDER;
tempItem.title = tempItem.title.substr(0, tempItem.path.length() - 1);
//_size = atof(Util::getXMLAttribute(xmlItem, "d:quota-used-bytes").c_str());
//TODO is always 0?
//Log::writeInfoLog("Size of folder " + std::to_string(_size));
//TODO set sync status of folders --> use sqlite?
}
else
{
//tempItem.type = Itemtype::IFILE;
tempItem.size = atof(Util::getXMLAttribute(xmlItem, "d:getcontentlength").c_str());
tempItem.fileType = Util::getXMLAttribute(xmlItem, "d:getcontenttype");
if (iv_access(tempItem.localPath.c_str(), W_OK) != 0) string localPath = getLocalPath(pathUrl);
{
//tempItem.state = FileState::ICLOUD;
}
else
{
//tempItem.state = FileState::ISYNCED;
}
}
tempItem.title = tempItem.title.substr(tempItem.title.find_last_of("/") + 1, tempItem.title.length());
Util::decodeUrl(tempItem.title);
tempItems.push_back(tempItem);
//TODO no creation mode known, fill in here
xmlItem = xmlItem.substr(end + endItem.length());
begin = xmlItem.find(beginItem);
}
if (tempItems.empty())
return {};
string localPath = getLocalPath(pathUrl);
if (iv_access(localPath.c_str(), W_OK) != 0)
{
//if the current folder does not exist locally, create it //if the current folder does not exist locally, create it
iv_buildpath(localPath.c_str()); if (iv_access(localPath.c_str(), W_OK) != 0)
} iv_buildpath(localPath.c_str());
else
{
//get items from local path
if (iv_access(localPath.c_str(), R_OK) != 0)
{
Log::writeInfoLog("Local structure of " + localPath + " found.");
}
//getLocalFileStructure(tempItems, localPath); return tempItems;
} }
return {};
//update the .structure file acording to items in the folder //TODO return empty items?
//do not use anymore
//localPath = localPath + NEXTCLOUD_STRUCTURE_EXTENSION;
//save xml to make the structure available offline
/*
ofstream outFile(localPath);
if (outFile)
{
outFile << readBuffer;
Log::writeInfoLog("Saved local copy of tree structure to " + localPath);
}
else
{
Log::writeInfoLog(localPath + " Couldnt save copy of tree structure locally.");
}
*/
//outFile.close();
return tempItems;
} }
string WebDAV::propfind(const string &pathUrl) string WebDAV::propfind(const string &pathUrl)
@ -195,7 +150,7 @@ string WebDAV::propfind(const string &pathUrl)
*/ */
auto _url = "test"; auto _url = Util::accessConfig(Action::IReadString, "url");
string readBuffer; string readBuffer;
CURLcode res; CURLcode res;
@ -204,22 +159,25 @@ string WebDAV::propfind(const string &pathUrl)
if (curl) if (curl)
{ {
string post = _username + ":" + _password; string post = _username + ":" + _password;
Log::writeInfoLog(post);
Log::writeInfoLog(_url + NEXTCLOUD_ROOT_PATH + pathUrl);
struct curl_slist *headers = NULL; struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Depth: 1"); headers = curl_slist_append(headers, "Depth: 1");
curl_easy_setopt(curl, CURLOPT_URL, (_url + pathUrl).c_str()); curl_easy_setopt(curl, CURLOPT_URL, (_url + NEXTCLOUD_ROOT_PATH + pathUrl).c_str());
curl_easy_setopt(curl, CURLOPT_USERPWD, post.c_str()); curl_easy_setopt(curl, CURLOPT_USERPWD, post.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PROPFIND"); curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PROPFIND");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Util::writeCallback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Util::writeCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "<\?xml version=\"1.0\" encoding=\"UTF-8\"\?> \
/* <d:propfind xmlns:d=\"DAV:\"><d:prop xmlns:oc=\"http://owncloud.org/ns\"> \
if (iv_access(CACERT_PATH.c_str(), R_OK) == 0) <d:getlastmodified/> \
curl_easy_setopt(curl, CURLOPT_CAINFO, CACERT_PATH.c_str()); <d:getcontenttype/> \
else <oc:size/> \
Log::writeErrorLog("could not find cacert"); <d:getetag/> \
*/ <oc:favorite/> \
</d:prop></d:propfind>");
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
@ -242,15 +200,15 @@ string WebDAV::propfind(const string &pathUrl)
break; break;
default: default:
Message(ICON_ERROR, "Error", ("An unknown error occured. Switching to offline modus. To work online turn on online modus in the menu. (Curl Response Code " + std::to_string(response_code) + ")").c_str(), 4000); Message(ICON_ERROR, "Error", ("An unknown error occured. Switching to offline modus. To work online turn on online modus in the menu. (Curl Response Code " + std::to_string(response_code) + ")").c_str(), 4000);
//TODO change default msg
} }
} }
else else
{ {
string response = std::string("An error occured. (") + curl_easy_strerror(res) + " (Curl Error Code: " + std::to_string(res) + ")). Please try again."; string response = std::string("An error occured. (") + curl_easy_strerror(res) + " (Curl Error Code: " + std::to_string(res) + ")). Please try again.";
if(res == 60) Log::writeErrorLog(response);
response = "Seems as if you are using Let's Encrypt Certs. Please follow the guide on Github (https://github.com/JuanJakobo/Pocketbook-Nextcloud-Client) to use a custom Cert Store on PB.";
Message(ICON_ERROR, "Error", response.c_str(), 4000); Message(ICON_ERROR, "Error", response.c_str(), 4000);
} }
} }
return NULL; return "";
} }

View File

@ -10,7 +10,7 @@
#ifndef WEBDAV #ifndef WEBDAV
#define WEBDAV #define WEBDAV
#include "webDAVItemModel.h" #include "webDAVModel.h"
#include "eventHandler.h" #include "eventHandler.h"
#include <string> #include <string>

View File

@ -1,5 +1,5 @@
//------------------------------------------------------------------ //------------------------------------------------------------------
// webDavFileItem.h // webDAVItem.h
// //
// Author: JuanJakobo // Author: JuanJakobo
// Date: 07.07.2022 // Date: 07.07.2022
@ -10,6 +10,8 @@
#define WEBDAVITEM #define WEBDAVITEM
#include "model.h" #include "model.h"
//TODO use own
#include "item.h"
#include <string> #include <string>
@ -34,8 +36,8 @@ struct WebDAVItem : Entry{
std::string path; std::string path;
std::string title; std::string title;
std::string localPath; std::string localPath;
//FileState state{FileState::ICLOUD}; FileState state{FileState::ICLOUD};
//Itemtype _type; Itemtype type;
std::string lastEditDate{"Error"}; std::string lastEditDate{"Error"};
double size; double size;
std::string fileType; std::string fileType;