diff --git a/binding.gyp b/binding.gyp index 35d5c1ac..ce0cf1f3 100644 --- a/binding.gyp +++ b/binding.gyp @@ -28,11 +28,13 @@ "native-src/sync_root_interface/callbacks/FetchData/TransferContext.cpp", "native-src/virtual_drive/Wrappers.cpp", "native-src/virtual_drive/connect_sync_root.cpp", + "native-src/virtual_drive/convert_to_placeholder.cpp", "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/hydrate_file.cpp", - "native-src/virtual_drive/register_sync_root.cpp" + "native-src/virtual_drive/register_sync_root/register_sync_root.cpp", + "native-src/virtual_drive/register_sync_root/register_sync_root_wrapper.cpp" ], "include_dirs": [ "include", @@ -40,7 +42,8 @@ "include/placeholders_interface", "include/sync_root_interface", "include/sync_root_interface/callbacks", - "include/virtual_drive" + "include/virtual_drive", + "include/virtual_drive/register_sync_root" ], "libraries": [ "-lCldApi.lib", diff --git a/dist/addon.node b/dist/addon.node index a77fa29a..8b58b9f6 100644 Binary files a/dist/addon.node and b/dist/addon.node differ diff --git a/include/sync_root_interface/SyncRoot.h b/include/sync_root_interface/SyncRoot.h index a45b00fb..267410c0 100644 --- a/include/sync_root_interface/SyncRoot.h +++ b/include/sync_root_interface/SyncRoot.h @@ -24,7 +24,6 @@ struct ItemInfo class SyncRoot { public: - static HRESULT RegisterSyncRoot(const wchar_t *syncRootPath, const wchar_t *providerName, const wchar_t *providerVersion, const GUID &providerId, const wchar_t *logoPath); static std::vector GetRegisteredSyncRoots(); static HRESULT ConnectSyncRoot(const wchar_t *syncRootPath, InputSyncCallbacks syncCallbacks, napi_env env, CF_CONNECTION_KEY *connectionKey); static HRESULT DisconnectSyncRoot(const wchar_t *syncRootPath); diff --git a/include/virtual_drive/convert_to_placeholder.h b/include/virtual_drive/convert_to_placeholder.h new file mode 100644 index 00000000..f9c610ff --- /dev/null +++ b/include/virtual_drive/convert_to_placeholder.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +napi_value convert_to_placeholder_impl(napi_env env, napi_callback_info args); diff --git a/include/virtual_drive/register_sync_root/register_sync_root.h b/include/virtual_drive/register_sync_root/register_sync_root.h new file mode 100644 index 00000000..2f143295 --- /dev/null +++ b/include/virtual_drive/register_sync_root/register_sync_root.h @@ -0,0 +1,9 @@ +#pragma once + +#include +#include +#include "stdafx.h" +#include +#include + +void register_sync_root(const wchar_t *syncRootPath, const wchar_t *providerName, const wchar_t *providerVersion, const wchar_t *providerId, const wchar_t *logoPath); \ No newline at end of file diff --git a/include/virtual_drive/register_sync_root/register_sync_root_wrapper.h b/include/virtual_drive/register_sync_root/register_sync_root_wrapper.h new file mode 100644 index 00000000..185014f4 --- /dev/null +++ b/include/virtual_drive/register_sync_root/register_sync_root_wrapper.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +napi_value register_sync_root_wrapper(napi_env env, napi_callback_info args); diff --git a/native-src/sync_root_interface/SyncRoot.cpp b/native-src/sync_root_interface/SyncRoot.cpp index 6c81293b..31529173 100644 --- a/native-src/sync_root_interface/SyncRoot.cpp +++ b/native-src/sync_root_interface/SyncRoot.cpp @@ -11,17 +11,6 @@ namespace fs = std::filesystem; CF_CONNECTION_KEY gloablConnectionKey; std::map connectionMap; -void AddCustomState( - _In_ winrt::IVector &customStates, - _In_ LPCWSTR displayNameResource, - _In_ int id) -{ - winrt::StorageProviderItemPropertyDefinition customState; - customState.DisplayNameResource(displayNameResource); - customState.Id(id); - customStates.Append(customState); -} - void SyncRoot::HydrateFile(const wchar_t *filePath) { wprintf(L"Hydration file started %ls\n", filePath); @@ -115,62 +104,6 @@ void SyncRoot::DehydrateFile(const wchar_t *filePath) } } -HRESULT SyncRoot::RegisterSyncRoot(const wchar_t *syncRootPath, const wchar_t *providerName, const wchar_t *providerVersion, const GUID &providerId, const wchar_t *logoPath) -{ - try - { - // Convert GUID to string for syncRootID - wchar_t syncRootID[39]; - StringFromGUID2(providerId, syncRootID, 39); - - winrt::StorageProviderSyncRootInfo info; - info.Id(syncRootID); - - auto folder = winrt::StorageFolder::GetFolderFromPathAsync(syncRootPath).get(); - info.Path(folder); - - // The string can be in any form acceptable to SHLoadIndirectString. - info.DisplayNameResource(providerName); - - std::wstring completeIconResource = std::wstring(logoPath) + L",0"; - - // This icon is just for the sample. You should provide your own branded icon here - info.IconResource(completeIconResource.c_str()); - info.HydrationPolicy(winrt::StorageProviderHydrationPolicy::Full); - info.HydrationPolicyModifier(winrt::StorageProviderHydrationPolicyModifier::None); - info.PopulationPolicy(winrt::StorageProviderPopulationPolicy::AlwaysFull); - info.InSyncPolicy(winrt::StorageProviderInSyncPolicy::FileCreationTime | winrt::StorageProviderInSyncPolicy::DirectoryCreationTime); - info.Version(providerVersion); - info.ShowSiblingsAsGroup(false); - info.HardlinkPolicy(winrt::StorageProviderHardlinkPolicy::None); - - 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); - - winrt::IBuffer contextBuffer = winrt::CryptographicBuffer::ConvertStringToBinary(syncRootIdentity.data(), winrt::BinaryStringEncoding::Utf8); - info.Context(contextBuffer); - - winrt::IVector customStates = info.StorageProviderItemPropertyDefinitions(); - AddCustomState(customStates, L"CustomStateName1", 1); - AddCustomState(customStates, L"CustomStateName2", 2); - AddCustomState(customStates, L"CustomStateName3", 3); - - winrt::StorageProviderSyncRootManager::Register(info); - - return S_OK; - } - catch (...) - { - wprintf(L"Could not register the sync root, hr %08x\n", static_cast(winrt::to_hresult())); - return E_FAIL; - } -} - std::vector SyncRoot::GetRegisteredSyncRoots() { std::vector syncRootList; diff --git a/native-src/virtual_drive/Wrappers.cpp b/native-src/virtual_drive/Wrappers.cpp index 6dfa45b8..05f7a7ec 100644 --- a/native-src/virtual_drive/Wrappers.cpp +++ b/native-src/virtual_drive/Wrappers.cpp @@ -8,12 +8,13 @@ #include #include #include -#include "register_sync_root.h" +#include "register_sync_root_wrapper.h" #include "create_folder_placeholder.h" #include "create_file_placeholder.h" #include "get_file_identity.h" #include "connect_sync_root.h" #include "hydrate_file.h" +#include "convert_to_placeholder.h" #include "NAPI_SAFE_WRAP.h" std::string WStringToUTF8(const std::wstring &wstr) @@ -81,7 +82,7 @@ napi_value UnregisterSyncRootWrapper(napi_env env, napi_callback_info args) } napi_value RegisterSyncRootWrapper(napi_env env, napi_callback_info info) { - return NAPI_SAFE_WRAP(env, info, register_sync_root_impl); + return NAPI_SAFE_WRAP(env, info, register_sync_root_wrapper); } napi_value GetRegisteredSyncRootsWrapper(napi_env env, napi_callback_info args) @@ -300,46 +301,8 @@ napi_value GetPlaceholderStateWrapper(napi_env env, napi_callback_info args) return result; } -napi_value ConvertToPlaceholderWrapper(napi_env env, napi_callback_info args) -{ - size_t argc = 2; - napi_value argv[2]; - napi_get_cb_info(env, args, &argc, argv, nullptr, nullptr); - - if (argc < 2) - { - napi_throw_type_error(env, nullptr, "Wrong number of arguments"); - return nullptr; - } - - char path[1024]; - char serverIdentity[1024]; - size_t pathLen, serverIdentityLen; - - napi_get_value_string_utf8(env, argv[0], path, sizeof(path), &pathLen); - napi_get_value_string_utf8(env, argv[1], serverIdentity, sizeof(serverIdentity), &serverIdentityLen); - - std::wstring wPath(path, path + pathLen); - std::wstring wServerIdentity(serverIdentity, serverIdentity + serverIdentityLen); - - PlaceholderResult result = Placeholders::ConvertToPlaceholder(wPath, wServerIdentity); - - napi_value resultObj; - napi_create_object(env, &resultObj); - - napi_value successValue; - napi_get_boolean(env, result.success, &successValue); - napi_set_named_property(env, resultObj, "success", successValue); - - if (!result.success) - { - std::string errorMessage(result.errorMessage.begin(), result.errorMessage.end()); - napi_value errorValue; - napi_create_string_utf8(env, errorMessage.c_str(), errorMessage.length(), &errorValue); - napi_set_named_property(env, resultObj, "errorMessage", errorValue); - } - - return resultObj; +napi_value ConvertToPlaceholderWrapper(napi_env env, napi_callback_info args) { + return NAPI_SAFE_WRAP(env, args, convert_to_placeholder_impl); } napi_value UpdateFileIdentityWrapper(napi_env env, napi_callback_info args) diff --git a/native-src/virtual_drive/convert_to_placeholder.cpp b/native-src/virtual_drive/convert_to_placeholder.cpp new file mode 100644 index 00000000..96a4c5b0 --- /dev/null +++ b/native-src/virtual_drive/convert_to_placeholder.cpp @@ -0,0 +1,41 @@ +#include +#include "Placeholders.h" + +napi_value convert_to_placeholder_impl(napi_env env, napi_callback_info args) +{ + size_t argc = 2; + napi_value argv[2]; + napi_get_cb_info(env, args, &argc, argv, nullptr, nullptr); + + size_t pathLen, serverIdentityLen; + napi_get_value_string_utf8(env, argv[0], nullptr, 0, &pathLen); + napi_get_value_string_utf8(env, argv[1], nullptr, 0, &serverIdentityLen); + + std::string path(pathLen, '\0'); + std::string serverIdentity(serverIdentityLen, '\0'); + + napi_get_value_string_utf8(env, argv[0], &path[0], pathLen + 1, nullptr); + napi_get_value_string_utf8(env, argv[1], &serverIdentity[0], serverIdentityLen + 1, nullptr); + + std::wstring wPath(path.begin(), path.end()); + std::wstring wServerIdentity(serverIdentity.begin(), serverIdentity.end()); + + PlaceholderResult result = Placeholders::ConvertToPlaceholder(wPath, wServerIdentity); + + napi_value resultObj; + napi_create_object(env, &resultObj); + + napi_value successValue; + napi_get_boolean(env, result.success, &successValue); + napi_set_named_property(env, resultObj, "success", successValue); + + if (!result.success) + { + std::string errorMessage(result.errorMessage.begin(), result.errorMessage.end()); + napi_value errorValue; + napi_create_string_utf8(env, errorMessage.c_str(), errorMessage.length(), &errorValue); + napi_set_named_property(env, resultObj, "errorMessage", errorValue); + } + + return resultObj; +} diff --git a/native-src/virtual_drive/register_sync_root/register_sync_root.cpp b/native-src/virtual_drive/register_sync_root/register_sync_root.cpp new file mode 100644 index 00000000..d1559268 --- /dev/null +++ b/native-src/virtual_drive/register_sync_root/register_sync_root.cpp @@ -0,0 +1,42 @@ +#include "Callbacks.h" +#include "SyncRoot.h" +#include "stdafx.h" +#include +#include "Logger.h" +#include +#include + +void register_sync_root(const wchar_t *syncRootPath, const wchar_t *providerName, const wchar_t *providerVersion, const wchar_t *providerId, const wchar_t *logoPath) +{ + winrt::StorageProviderSyncRootInfo info; + info.Id(providerId); + + auto folder = winrt::StorageFolder::GetFolderFromPathAsync(syncRootPath).get(); + info.Path(folder); + + info.DisplayNameResource(providerName); + + std::wstring completeIconResource = std::wstring(logoPath) + L",0"; + info.IconResource(completeIconResource); + + info.HydrationPolicy(winrt::StorageProviderHydrationPolicy::Full); + info.HydrationPolicyModifier(winrt::StorageProviderHydrationPolicyModifier::None); + info.PopulationPolicy(winrt::StorageProviderPopulationPolicy::AlwaysFull); + info.InSyncPolicy(winrt::StorageProviderInSyncPolicy::FileCreationTime | winrt::StorageProviderInSyncPolicy::DirectoryCreationTime); + info.Version(providerVersion); + info.ShowSiblingsAsGroup(false); + info.HardlinkPolicy(winrt::StorageProviderHardlinkPolicy::None); + + 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); + + winrt::IBuffer contextBuffer = winrt::CryptographicBuffer::ConvertStringToBinary(syncRootIdentity, winrt::BinaryStringEncoding::Utf8); + info.Context(contextBuffer); + + winrt::StorageProviderSyncRootManager::Register(info); +} diff --git a/native-src/virtual_drive/register_sync_root/register_sync_root_wrapper.cpp b/native-src/virtual_drive/register_sync_root/register_sync_root_wrapper.cpp new file mode 100644 index 00000000..14976cd6 --- /dev/null +++ b/native-src/virtual_drive/register_sync_root/register_sync_root_wrapper.cpp @@ -0,0 +1,38 @@ +#include +#include "register_sync_root.h" + +napi_value register_sync_root_wrapper(napi_env env, napi_callback_info args) +{ + size_t argc = 5; + napi_value argv[5]; + napi_get_cb_info(env, args, &argc, argv, nullptr, nullptr); + + size_t syncRootPathLength; + napi_get_value_string_utf16(env, argv[0], nullptr, 0, &syncRootPathLength); + std::wstring syncRootPath(syncRootPathLength, L'\0'); + napi_get_value_string_utf16(env, argv[0], reinterpret_cast(&syncRootPath[0]), syncRootPathLength + 1, nullptr); + + size_t providerNameLength; + napi_get_value_string_utf16(env, argv[1], nullptr, 0, &providerNameLength); + std::wstring providerName(providerNameLength, L'\0'); + napi_get_value_string_utf16(env, argv[1], reinterpret_cast(&providerName[0]), providerNameLength + 1, nullptr); + + size_t providerVersionLength; + napi_get_value_string_utf16(env, argv[2], nullptr, 0, &providerVersionLength); + std::wstring providerVersion(providerVersionLength, L'\0'); + napi_get_value_string_utf16(env, argv[2], reinterpret_cast(&providerVersion[0]), providerVersionLength + 1, nullptr); + + size_t providerIdLength; + napi_get_value_string_utf16(env, argv[3], nullptr, 0, &providerIdLength); + std::wstring providerId(providerIdLength, L'\0'); + napi_get_value_string_utf16(env, argv[3], reinterpret_cast(&providerId[0]), providerIdLength + 1, nullptr); + + size_t logoPathLength; + napi_get_value_string_utf16(env, argv[4], nullptr, 0, &logoPathLength); + std::wstring logoPath(logoPathLength, L'\0'); + napi_get_value_string_utf16(env, argv[4], reinterpret_cast(&logoPath[0]), logoPathLength + 1, nullptr); + + register_sync_root(syncRootPath.c_str(), providerName.c_str(), providerVersion.c_str(), providerId.c_str(), logoPath.c_str()); + + return nullptr; +}