Skip to content
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"prettier": "^2.0.5",
"ts-loader": "^8.0.0",
"ts-node": "^9.0.0",
"typedoc": "^0.19.2",
"typedoc": "^0.19.2",
"typescript": "^4.0.3"
},
"bugs": {
Expand Down
99 changes: 78 additions & 21 deletions src/share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import Client, { ClientError } from "./client";
import File from "./file";
import FileSystemElement from "./fileSystemElement";
import Folder from "./folder";
import UserGroup from "./userGroup";
import User from "./user";

export enum SharePermission {
all = 31,
Expand All @@ -12,7 +14,7 @@ export enum SharePermission {
share = 16,
}

enum ShareType {
export enum ShareType {
user = 0,
group = 1,
publicLink = 3,
Expand All @@ -21,9 +23,10 @@ enum ShareType {

export interface ICreateShare {
"fileSystemElement": FileSystemElement;
// @todo "shareWith"?: User | UserGroup | EMail;
"publicUpload"?: boolean;
"password"?: string;
"shareType"?: ShareType;
"shareWith"?: User | UserGroup;
}

export enum ShareItemType {
Expand All @@ -39,11 +42,18 @@ export default class Share {
}

public static createShareRequestBody(createShare: ICreateShare): string {
const shareType: ShareType = ShareType.publicLink;
// for backwards compatibility, default sharetype is public link
const shareType: ShareType = createShare.shareType == null ? ShareType.publicLink : createShare.shareType;
if (shareType === ShareType.user || shareType === ShareType.group) {
if (createShare.shareWith == null) {
throw new ClientError("Error, when using ShareType user or usergroup, you need to specify shareWith", "ERR_SHARETYPE_USER_GROUP_WITHOUT_SHAREWITH");
}
}

const shareRequest: {
path: string,
shareType: number,
shareWith?: string,
// @todo permissions: number | number[]
password?: string,
} = {
Expand All @@ -52,6 +62,10 @@ export default class Share {
shareType,
};

if (createShare.shareWith) {
shareRequest.shareWith = createShare.shareWith.id;
}

if (createShare.password) {
shareRequest.password = createShare.password;
}
Expand All @@ -65,10 +79,10 @@ export default class Share {
id: string;
itemType: ShareItemType,
note: string,
token: string,
url: string,
token: string | null,
url: string | null,
publicUpload: boolean,
// share_type: number,
shareType: number,
// "uid_owner": string,
// "displayname_owner": string,
// "permissions": SharePermission,
Expand All @@ -81,7 +95,7 @@ export default class Share {
// "displayname_file_owner": string,
// "path": string,
// "mimetype"?: string,
// "share_with"?: string,
"shareWith": User | UserGroup | null,
// "share_with_displayname"?: string,
// "mail_send": boolean,
// "hide_download": boolean,
Expand All @@ -94,9 +108,10 @@ export default class Share {
id,
itemType: ShareItemType.file,
note: "",
token: "",
url: "",
token: null,
url: null,
publicUpload: false,
shareType: ShareType.publicLink,
};
}

Expand All @@ -111,6 +126,7 @@ export default class Share {

/**
* set a new password
*
* @param password
*/
public async setPassword(password: string): Promise<void> {
Expand All @@ -136,16 +152,6 @@ export default class Share {
throw new ClientError(`Error invalid share data received "ocs.data" missing`, "ERR_INVALID_SHARE_RESPONSE");
}

if (!rawShareData.ocs.data[0].url) {
throw new ClientError(`Error invalid share data received "url" missing`, "ERR_INVALID_SHARE_RESPONSE");
}
this.memento.url = rawShareData.ocs.data[0].url;

if (!rawShareData.ocs.data[0].token) {
throw new ClientError(`Error invalid share data received "token" missing`, "ERR_INVALID_SHARE_RESPONSE");
}
this.memento.token = rawShareData.ocs.data[0].token;

if (!rawShareData.ocs.data[0].item_type) {
throw new ClientError(`Error invalid share data received "item_type" missing`, "ERR_INVALID_SHARE_RESPONSE");
}
Expand All @@ -163,6 +169,42 @@ export default class Share {
this.memento.note = rawShareData.ocs.data[0].note;
}

if (rawShareData.ocs.data[0].share_type) {
switch (rawShareData.ocs.data[0].share_type) {
case ShareType.user.valueOf():
this.memento.shareType = ShareType.user;
if (rawShareData.ocs.data[0].share_with) {
this.memento.shareWith = new User(this.client, rawShareData.ocs.data[0].share_with)
} else {
throw new ClientError(`Error user share without target user`, "ERR_USER_SHARE_NO_USER");
}
break;
case ShareType.group.valueOf():
this.memento.shareType = ShareType.group;
if (rawShareData.ocs.data[0].share_with) {
this.memento.shareWith = new UserGroup(this.client, rawShareData.ocs.data[0].share_with)
} else {
throw new ClientError(`Error usergroup share without target usergroup`, "ERR_USERGROUP_SHARE_NO_USERGROUP");
}
break;
case ShareType.publicLink.valueOf():
this.memento.shareType = ShareType.publicLink;
if (!rawShareData.ocs.data[0].url) {
throw new ClientError(`Error invalid share data received "url" missing`, "ERR_INVALID_SHARE_RESPONSE");
}
this.memento.url = rawShareData.ocs.data[0].url;
if (!rawShareData.ocs.data[0].token) {
throw new ClientError(`Error invalid share data received "token" missing`, "ERR_INVALID_SHARE_RESPONSE");
}
this.memento.token = rawShareData.ocs.data[0].token;

break;
default:
throw new ClientError(`Error unsupported share type`, "ERR_UNSUPPORTED_SHARE_TYPE");
}

}

// console.log(JSON.stringify(rawShareData, null, 4));
// console.log(JSON.stringify(this, null, 4));
}
Expand All @@ -171,15 +213,15 @@ export default class Share {
* token
* The token is readonly
*/
public get token(): string {
public get token(): string | null {
return this.memento.token;
}

/**
* share url
* The share url is readonly
*/
public get url(): string {
public get url(): string | null {
return this.memento.url;
}

Expand Down Expand Up @@ -222,4 +264,19 @@ export default class Share {
return this.memento.itemType;
}

/**
* share type
* The type of the share
*/
public get shareType(): ShareType {
return this.memento.shareType;
}

/**
* share with
* The entity the share is shared with
*/
public get shareWith(): User | UserGroup | null {
return this.memento.shareWith;
}
}