Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"native-src/virtual_drive/create_file_placeholder.cpp",
"native-src/virtual_drive/create_folder_placeholder.cpp",
"native-src/virtual_drive/get_file_identity.cpp",
"native-src/virtual_drive/get_registered_sync_roots/get_registered_sync_roots.cpp",
"native-src/virtual_drive/get_registered_sync_roots/get_registered_sync_roots_wrapper.cpp",
"native-src/virtual_drive/hydrate_file.cpp",
"native-src/virtual_drive/register_sync_root/register_sync_root.cpp",
"native-src/virtual_drive/register_sync_root/register_sync_root_wrapper.cpp"
Expand All @@ -43,6 +45,7 @@
"include/sync_root_interface",
"include/sync_root_interface/callbacks",
"include/virtual_drive",
"include/virtual_drive/get_registered_sync_roots",
"include/virtual_drive/register_sync_root"
],
"libraries": [
Expand Down
Binary file modified dist/addon.node
Binary file not shown.
9 changes: 0 additions & 9 deletions include/sync_root_interface/SyncRoot.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@
#include <iostream>
#include <vector>

struct SyncRoots
{
std::wstring id;
std::wstring path;
std::wstring displayName;
std::wstring version;
};

struct ItemInfo
{
std::wstring path;
Expand All @@ -24,7 +16,6 @@ struct ItemInfo
class SyncRoot
{
public:
static std::vector<SyncRoots> GetRegisteredSyncRoots();
static HRESULT ConnectSyncRoot(const wchar_t *syncRootPath, InputSyncCallbacks syncCallbacks, napi_env env, CF_CONNECTION_KEY *connectionKey);
static HRESULT DisconnectSyncRoot(const wchar_t *syncRootPath);
static HRESULT UnregisterSyncRoot(const GUID &providerId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <iostream>
#include <vector>

struct SyncRoots
{
std::wstring id;
std::wstring path;
std::wstring displayName;
std::wstring version;
std::wstring context;
};

std::vector<SyncRoots> get_registered_sync_roots();
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <node_api.h>

napi_value get_registered_sync_roots_wrapper(napi_env env, napi_callback_info args);
45 changes: 0 additions & 45 deletions native-src/sync_root_interface/SyncRoot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,51 +104,6 @@ void SyncRoot::DehydrateFile(const wchar_t *filePath)
}
}

std::vector<SyncRoots> SyncRoot::GetRegisteredSyncRoots()
{
std::vector<SyncRoots> syncRootList;
try
{
auto syncRoots = winrt::StorageProviderSyncRootManager::GetCurrentSyncRoots();

printf("Sync roots count: %d\n", syncRoots.Size());

for (auto const &info : syncRoots)
{
auto contextBuffer = info.Context();
std::wstring contextString;
if (contextBuffer)
{
contextString = winrt::CryptographicBuffer::ConvertBinaryToString(
winrt::BinaryStringEncoding::Utf8,
contextBuffer)
.c_str();
}

/**
* v2.5.1 Jonathan Arce
* Sync root register are now filtered using the characters '->' and '#inxt#' to identify our register.
* Currently, we only use '#inxt#' in the register, but to support previous versions, we are still
* including '->' in the filter. In future versions, the filtering by '->' should be removed.
*/
if (contextString.find(L"#inxt#") != std::wstring::npos || contextString.find(L"->") != std::wstring::npos)
{
SyncRoots sr;
sr.id = info.Id();
sr.path = info.Path().Path();
sr.displayName = info.DisplayNameResource();
sr.version = info.Version();
syncRootList.push_back(sr);
}
}
}
catch (...)
{
Logger::getInstance().log("ERROR: getting sync root", LogLevel::INFO);
}
return syncRootList;
}

HRESULT SyncRoot::UnregisterSyncRoot(const GUID &providerId)
{
try
Expand Down
78 changes: 5 additions & 73 deletions native-src/virtual_drive/Wrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@
#include "connect_sync_root.h"
#include "hydrate_file.h"
#include "convert_to_placeholder.h"
#include "get_registered_sync_roots_wrapper.h"
#include "NAPI_SAFE_WRAP.h"

std::string WStringToUTF8(const std::wstring &wstr)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
return conv.to_bytes(wstr);
}

napi_value CreatePlaceholderFile(napi_env env, napi_callback_info args)
{
return NAPI_SAFE_WRAP(env, args, create_file_placeholder_impl);
Expand Down Expand Up @@ -85,78 +80,15 @@ napi_value RegisterSyncRootWrapper(napi_env env, napi_callback_info info) {
return NAPI_SAFE_WRAP(env, info, register_sync_root_wrapper);
}

napi_value GetRegisteredSyncRootsWrapper(napi_env env, napi_callback_info args)
{
try
{
std::vector<SyncRoots> roots = SyncRoot::GetRegisteredSyncRoots();

napi_value jsArray;
napi_status status = napi_create_array_with_length(env, roots.size(), &jsArray);
if (status != napi_ok)
throw std::runtime_error("Error creating the array");

for (size_t i = 0; i < roots.size(); i++)
{
napi_value jsObj;
status = napi_create_object(env, &jsObj);
if (status != napi_ok)
throw std::runtime_error("Error creating the object");

std::string id = WStringToUTF8(roots[i].id);
napi_value napiId;
status = napi_create_string_utf8(env, id.c_str(), id.size(), &napiId);
if (status != napi_ok)
throw std::runtime_error("Error creating the string id");
napi_set_named_property(env, jsObj, "id", napiId);

std::string path = WStringToUTF8(roots[i].path);
napi_value napiPath;
status = napi_create_string_utf8(env, path.c_str(), path.size(), &napiPath);
if (status != napi_ok)
throw std::runtime_error("Error creating the string path");
napi_set_named_property(env, jsObj, "path", napiPath);

std::string displayName = WStringToUTF8(roots[i].displayName);
napi_value napiDisplayName;
status = napi_create_string_utf8(env, displayName.c_str(), displayName.size(), &napiDisplayName);
if (status != napi_ok)
throw std::runtime_error("Error creating the string displayName");
napi_set_named_property(env, jsObj, "displayName", napiDisplayName);

std::string version = WStringToUTF8(roots[i].version);
napi_value napiVersion;
status = napi_create_string_utf8(env, version.c_str(), version.size(), &napiVersion);
if (status != napi_ok)
throw std::runtime_error("Error creating the string version");
napi_set_named_property(env, jsObj, "version", napiVersion);

status = napi_set_element(env, jsArray, i, jsObj);
if (status != napi_ok)
throw std::runtime_error("Error setting the element in the array");
}

return jsArray;
}
catch (const std::exception &ex)
{
napi_throw_error(env, nullptr, ex.what());
return nullptr;
}
catch (...)
{
napi_throw_error(env, nullptr, "An unknown error occurred in GetRegisteredSyncRootsWrapper");
return nullptr;
}
napi_value GetRegisteredSyncRootsWrapper(napi_env env, napi_callback_info args) {
return NAPI_SAFE_WRAP(env, args, get_registered_sync_roots_wrapper);
}

napi_value ConnectSyncRootWrapper(napi_env env, napi_callback_info args)
{
napi_value ConnectSyncRootWrapper(napi_env env, napi_callback_info args) {
return NAPI_SAFE_WRAP(env, args, connect_sync_root_impl);
}

napi_value CreateEntryWrapper(napi_env env, napi_callback_info args)
{
napi_value CreateEntryWrapper(napi_env env, napi_callback_info args) {
return NAPI_SAFE_WRAP(env, args, create_folder_placeholder_impl);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "stdafx.h"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function returns an array of SyncRoots. We obtain them from windows using winrt and iterate them obtaining their info.

#include <filesystem>
#include <iostream>
#include <vector>
#include "get_registered_sync_roots.h"

std::vector<SyncRoots> get_registered_sync_roots() {
std::vector<SyncRoots> syncRootList;

auto syncRoots = winrt::StorageProviderSyncRootManager::GetCurrentSyncRoots();

for (auto const &info : syncRoots) {
SyncRoots sr;
sr.id = info.Id();
sr.path = info.Path().Path();
sr.displayName = info.DisplayNameResource();
sr.version = info.Version();

auto contextBuffer = info.Context();
if (contextBuffer) {
sr.context = winrt::CryptographicBuffer::ConvertBinaryToString(winrt::BinaryStringEncoding::Utf8, contextBuffer).c_str();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously here we were filtering by context using #inxt#, however now we do this filter in javascript using the displayName and the path instead.

syncRootList.push_back(sr);
}

return syncRootList;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <locale>
#include <codecvt>
#include <windows.h>
#include <node_api.h>
#include "get_registered_sync_roots.h"

std::string WStringToUTF8(const std::wstring &wstr) {
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
return conv.to_bytes(wstr);
}

void add_string_property(napi_env env, napi_value obj, const char* key, const std::wstring& value) {
std::string utf8Value = WStringToUTF8(value);
napi_value napiValue;
napi_create_string_utf8(env, utf8Value.c_str(), utf8Value.size(), &napiValue);
napi_set_named_property(env, obj, key, napiValue);
}

napi_value get_registered_sync_roots_wrapper(napi_env env, napi_callback_info args) {
std::vector<SyncRoots> roots = get_registered_sync_roots();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we just parse the sync roots obtained before to a napi vector that javascript can understand.


napi_value jsArray;
napi_create_array_with_length(env, roots.size(), &jsArray);

for (size_t i = 0; i < roots.size(); i++) {
napi_value jsObj;
napi_create_object(env, &jsObj);

add_string_property(env, jsObj, "id", roots[i].id);
add_string_property(env, jsObj, "path", roots[i].path);
add_string_property(env, jsObj, "displayName", roots[i].displayName);
add_string_property(env, jsObj, "version", roots[i].version);
add_string_property(env, jsObj, "context", roots[i].context);

napi_set_element(env, jsArray, i, jsObj);
}

return jsArray;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ void register_sync_root(const wchar_t *syncRootPath, const wchar_t *providerName
winrt::Uri uri(L"https://drive.internxt.com/app/trash");
info.RecycleBinUri(uri);

// Context
std::wstring syncRootIdentity(syncRootPath);
syncRootIdentity.append(L"#inxt#");
syncRootIdentity.append(providerName);
Expand Down