Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hash windows paths #1172

Merged
merged 14 commits into from Sep 27, 2019
2 changes: 1 addition & 1 deletion Jenkinsfile
Expand Up @@ -101,7 +101,7 @@ pipeline {
steps {
nvm(env.NODE_VERSION) {
// Install dependencies
sh 'npm install'
sh 'npm ci'
nirinchev marked this conversation as resolved.
Show resolved Hide resolved
// Update the version
script {
if (PUBLISH == 'true') {
Expand Down
3 changes: 2 additions & 1 deletion RELEASENOTES.md
Expand Up @@ -2,7 +2,8 @@

### Enhancements

- None
- Added hashing of the paths that Realms gets downloaded to on Windows. This is to avoid hitting the 260 character limit. ([#1172](https://github.com/realm/realm-studio/pull/1172))
- Added a menu item to "Copy local Realm path" to the users clipboard. ([#1172](https://github.com/realm/realm-studio/pull/1172))

### Fixed

Expand Down
27 changes: 27 additions & 0 deletions src/services/ros/realms.ts
Expand Up @@ -18,6 +18,9 @@

import * as Realm from 'realm';

import * as crypto from 'crypto';
import * as fs from 'fs-extra';
import * as path from 'path';
import { fetchAuthenticated, IRealmFile, RealmType, UserStatus } from '.';
import { showError } from '../../ui/reusable/errors';

Expand Down Expand Up @@ -49,9 +52,11 @@ export const open = async (params: {
const url = getUrl(params.user, params.realmPath);

let clientResetOcurred = false;

const realmPromise = Realm.open({
encryptionKey: params.encryptionKey,
schema: params.schema,
path: getPathOnDisk(params.user, params.realmPath),
sync: {
url,
user: params.user,
Expand Down Expand Up @@ -82,6 +87,27 @@ export const open = async (params: {
return realm;
};

// We rewrite the path on disk on Windows because default realm paths
// may hit the 260 character limits, especially with partial realms.
const getPathOnDisk = (
nirinchev marked this conversation as resolved.
Show resolved Hide resolved
user: Realm.Sync.User,
realmPath: string,
): string | undefined => {
// Only Windows have path limits, so it's fine to default to whatever OS generates.
if (process.platform === 'win32') {
const userPath = path.join(process.cwd(), user.identity);
fs.ensureDirSync(userPath);

return path.join(
userPath,
crypto
.createHash('md5')
.update(realmPath)
.digest('hex'),
);
}
};

export const create = (
user: Realm.Sync.User,
realmPath: string,
Expand All @@ -101,6 +127,7 @@ export const create = (
ssl: { validate: validateCertificates },
},
schema,
path: getPathOnDisk(user, realmPath),
}).then(resolve, reject);
});
};
Expand Down
24 changes: 23 additions & 1 deletion src/ui/RealmBrowser/index.tsx
Expand Up @@ -253,6 +253,15 @@ class RealmBrowserContainer
],
};

const copyRealmPathItem: MenuItemConstructorOptions = {
label: 'Copy local Realm path',
click: () => {
this.copyRealmPathToClipboard().then(null, err => {
showError('Failed to copy Realm path', err);
});
},
};

return menu.performModifications(template, [
{
action: 'append',
Expand All @@ -262,7 +271,12 @@ class RealmBrowserContainer
{
action: 'prepend',
id: 'close',
items: [exportSchemaMenu, exportDataMenu, { type: 'separator' }],
items: [
exportSchemaMenu,
exportDataMenu,
copyRealmPathItem,
{ type: 'separator' },
],
},
{
action: 'append',
Expand Down Expand Up @@ -728,6 +742,14 @@ class RealmBrowserContainer
}
}
}

private async copyRealmPathToClipboard(): Promise<void> {
if (this.realm) {
await navigator.clipboard.writeText(this.realm.path);
} else {
throw new Error('Realm was not loaded');
}
}
}

export { RealmBrowserContainer as RealmBrowser };