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
//save all to sqlite
Log::writeInfoLog(NEXTCLOUD_PATH);
if (iv_access(NEXTCLOUD_PATH.c_str(), W_OK) != 0)
iv_mkdir(NEXTCLOUD_PATH.c_str(), 0777);
Log::writeInfoLog(NEXTCLOUD_FILE_PATH);
if (iv_access(NEXTCLOUD_FILE_PATH.c_str(), W_OK) != 0)
iv_mkdir(NEXTCLOUD_FILE_PATH.c_str(), 0777);
Log::writeInfoLog(CONFIG_PATH);
if (iv_access(CONFIG_PATH.c_str(), W_OK) == 0)
{
_username = Util::accessConfig(Action::IReadSecret,"Username");
_password = Util::accessConfig(Action::IReadSecret,"Password");
_username = Util::accessConfig(Action::IReadString,"username");
_password = Util::accessConfig(Action::IReadSecret,"password");
}
if(_username.empty())
Log::writeErrorLog("username not set");
//test login
//
//
//create database
}
//TODO pas as reversne and no return
@ -57,125 +57,80 @@ string WebDAV::getLocalPath(string 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)
{
string xmlItem = propfind(pathUrl);
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)
if(!xmlItem.empty())
{
end = xmlItem.find(endItem);
//tempItems.push_back(xml.substr(begin, end));
string beginItem = "<d:response>";
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");
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)
while (begin != std::string::npos)
{
tempItem.path.erase(0,tempItem.path.find(NEXTCLOUD_START_PATH));
Log::writeInfoLog("path after transformation " + tempItem.path);
end = xmlItem.find(endItem);
//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() == '/')
{
tempItem.localPath = tempItem.localPath.substr(0, tempItem.localPath.length() - 1);
//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");
//TODO doppelt
if (tempItems.empty())
return {};
if (iv_access(tempItem.localPath.c_str(), W_OK) != 0)
{
//tempItem.state = FileState::ICLOUD;
}
else
{
//tempItem.state = FileState::ISYNCED;
}
}
string localPath = getLocalPath(pathUrl);
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
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.");
}
if (iv_access(localPath.c_str(), W_OK) != 0)
iv_buildpath(localPath.c_str());
//getLocalFileStructure(tempItems, localPath);
return tempItems;
}
//update the .structure file acording to items in the folder
//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;
return {};
//TODO return empty items?
}
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;
CURLcode res;
@ -204,22 +159,25 @@ string WebDAV::propfind(const string &pathUrl)
if (curl)
{
string post = _username + ":" + _password;
Log::writeInfoLog(post);
Log::writeInfoLog(_url + NEXTCLOUD_ROOT_PATH + pathUrl);
struct curl_slist *headers = NULL;
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_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PROPFIND");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Util::writeCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
/*
if (iv_access(CACERT_PATH.c_str(), R_OK) == 0)
curl_easy_setopt(curl, CURLOPT_CAINFO, CACERT_PATH.c_str());
else
Log::writeErrorLog("could not find cacert");
*/
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\"> \
<d:getlastmodified/> \
<d:getcontenttype/> \
<oc:size/> \
<d:getetag/> \
<oc:favorite/> \
</d:prop></d:propfind>");
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
@ -242,15 +200,15 @@ string WebDAV::propfind(const string &pathUrl)
break;
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);
//TODO change default msg
}
}
else
{
string response = std::string("An error occured. (") + curl_easy_strerror(res) + " (Curl Error Code: " + std::to_string(res) + ")). Please try again.";
if(res == 60)
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.";
Log::writeErrorLog(response);
Message(ICON_ERROR, "Error", response.c_str(), 4000);
}
}
return NULL;
return "";
}

View File

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

View File

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