guratan 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/dist/cli.d.ts +25 -3
- package/dist/cli.js +40 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/main.js +152 -13
- package/dist/tdrive.d.ts +12 -0
- package/dist/tdrive.js +25 -0
- package/dist/tsend.d.ts +42 -19
- package/dist/tsend.js +43 -55
- package/dist/tshare.d.ts +71 -0
- package/dist/tshare.js +86 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -4,13 +4,31 @@ Google Drive へファイルを送信する簡易ツール。
|
|
|
4
4
|
|
|
5
5
|
## Usage
|
|
6
6
|
|
|
7
|
+
### send
|
|
8
|
+
|
|
7
9
|
```
|
|
8
10
|
$ GOOGLE_APPLICATION_CREDENTIALS=./gha-creds-temp.json npx guratan send --parent-id 12345ABC --dest-file-name test.txt --src-file-name path/to/test.txt
|
|
9
11
|
```
|
|
10
12
|
|
|
11
13
|
- `GOOGLE_APPLICATION_CREDENTIALS` にはサービスアカウントの鍵ファイルを指定
|
|
12
14
|
- 送信先フォルダー(`--parent-id`) に同名ファイル(`--dest-file-name`) が存在するときは上書きされる
|
|
15
|
+
- 同名ファイルが複数ある場合は最初にヒットしたものが上書きされる
|
|
16
|
+
- `--parent-id` `--dest-file-name` の代わりに `--file-id` で上書きファイルを指定できる
|
|
17
|
+
- 各オプションは環境変数での指定も可能(例. `--parent-id` = `GURATAN_PARENT_ID`)
|
|
18
|
+
- `guratan` からは upload と update のみ可能
|
|
19
|
+
|
|
20
|
+
### share
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
$ GOOGLE_APPLICATION_CREDENTIALS=./gha-creds-temp.json npx guratan share --file-id 12345ABC --type anyone --role reader
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
- `GOOGLE_APPLICATION_CREDENTIALS` にはサービスアカウントの鍵ファイルを指定
|
|
27
|
+
- `--file-id` の permission を作成/上書きすることで共有設定を変更する
|
|
28
|
+
- `--file-id` の代わりに `--parent-id` `--dest-file-name` で変更ファイルを指定できる
|
|
29
|
+
- 同名ファイルが複数ある場合は最初にヒットしたもののみが変更される
|
|
13
30
|
- 各オプションは環境変数での指定も可能(例. `--parent-id` = `GURATAN_PARENT_ID`)
|
|
31
|
+
- `guratan` から permission の削除はできない
|
|
14
32
|
|
|
15
33
|
## License
|
|
16
34
|
|
package/dist/cli.d.ts
CHANGED
|
@@ -1,12 +1,34 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Writable } from 'stream';
|
|
3
3
|
declare type Opts = {
|
|
4
|
+
stdout: Writable;
|
|
5
|
+
stderr: Writable;
|
|
6
|
+
};
|
|
7
|
+
declare type OptsSend = Opts & {
|
|
8
|
+
fileId: string;
|
|
4
9
|
parentId: string;
|
|
5
10
|
destFileName: string;
|
|
6
11
|
srcFileName: string;
|
|
12
|
+
destMimeType: string;
|
|
13
|
+
srcMimeType: string;
|
|
14
|
+
printId: boolean;
|
|
15
|
+
};
|
|
16
|
+
declare type OptsShare = Opts & {
|
|
17
|
+
fileId: string;
|
|
18
|
+
parentId: string;
|
|
19
|
+
destFileName: string;
|
|
20
|
+
type: string;
|
|
21
|
+
role: string;
|
|
22
|
+
emailAddress: string;
|
|
23
|
+
domain: string;
|
|
24
|
+
allowFileDiscovery?: boolean;
|
|
25
|
+
view: string;
|
|
26
|
+
moveToNewOwnersRoot?: boolean;
|
|
27
|
+
transferOwnership?: boolean;
|
|
28
|
+
sendNotificationEmail?: boolean;
|
|
29
|
+
emailMessage: string;
|
|
7
30
|
printId: boolean;
|
|
8
|
-
stdout: Writable;
|
|
9
|
-
stderr: Writable;
|
|
10
31
|
};
|
|
11
|
-
export declare const
|
|
32
|
+
export declare const cliSend: ({ fileId, parentId, destFileName, srcFileName, destMimeType, srcMimeType, printId, stdout, stderr }: OptsSend) => Promise<number>;
|
|
33
|
+
export declare const cliShare: ({ fileId, parentId, destFileName, type, role, emailAddress, domain, allowFileDiscovery, view, moveToNewOwnersRoot, transferOwnership, sendNotificationEmail, emailMessage, printId, stdout, stderr }: OptsShare) => Promise<number>;
|
|
12
34
|
export {};
|
package/dist/cli.js
CHANGED
|
@@ -1,7 +1,44 @@
|
|
|
1
|
-
import { driveClient
|
|
2
|
-
|
|
1
|
+
import { driveClient } from './tdrive.js';
|
|
2
|
+
import { sendFile } from './tsend.js';
|
|
3
|
+
import { createPermisson } from './tshare.js';
|
|
4
|
+
export const cliSend = async ({ fileId, parentId, destFileName, srcFileName, destMimeType, srcMimeType, printId, stdout, stderr }) => {
|
|
3
5
|
try {
|
|
4
|
-
const id = await sendFile(driveClient(),
|
|
6
|
+
const id = await sendFile(driveClient(), {
|
|
7
|
+
fileId,
|
|
8
|
+
parentId,
|
|
9
|
+
destFileName,
|
|
10
|
+
srcFileName,
|
|
11
|
+
destMimeType,
|
|
12
|
+
srcMimeType
|
|
13
|
+
});
|
|
14
|
+
if (printId) {
|
|
15
|
+
stdout.write(id);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
catch (err) {
|
|
19
|
+
stderr.write(err.toString());
|
|
20
|
+
stderr.write('\n');
|
|
21
|
+
return 1;
|
|
22
|
+
}
|
|
23
|
+
return 0;
|
|
24
|
+
};
|
|
25
|
+
export const cliShare = async ({ fileId, parentId, destFileName, type, role, emailAddress, domain, allowFileDiscovery, view, moveToNewOwnersRoot, transferOwnership, sendNotificationEmail, emailMessage, printId, stdout, stderr }) => {
|
|
26
|
+
try {
|
|
27
|
+
const id = await createPermisson(driveClient(), {
|
|
28
|
+
fileId,
|
|
29
|
+
parentId,
|
|
30
|
+
destFileName,
|
|
31
|
+
type,
|
|
32
|
+
role,
|
|
33
|
+
emailAddress,
|
|
34
|
+
domain,
|
|
35
|
+
allowFileDiscovery,
|
|
36
|
+
view,
|
|
37
|
+
moveToNewOwnersRoot,
|
|
38
|
+
transferOwnership,
|
|
39
|
+
sendNotificationEmail,
|
|
40
|
+
emailMessage
|
|
41
|
+
});
|
|
5
42
|
if (printId) {
|
|
6
43
|
stdout.write(id);
|
|
7
44
|
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/main.js
CHANGED
|
@@ -1,24 +1,130 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import yargs from 'yargs';
|
|
3
3
|
import { hideBin } from 'yargs/helpers';
|
|
4
|
-
import {
|
|
4
|
+
import { cliSend, cliShare } from './cli.js';
|
|
5
5
|
const envVarsPrefix = process.env['GURATAN_ENV_VARS_PREFIX'] || 'GURATAN';
|
|
6
6
|
const argv = await yargs(hideBin(process.argv))
|
|
7
7
|
.scriptName('guratan')
|
|
8
8
|
.env(envVarsPrefix)
|
|
9
|
-
.command('
|
|
9
|
+
.command('send [OPTIONS]', 'send file to folder in Google Drive', (yargs) => {
|
|
10
10
|
return yargs.options({
|
|
11
|
+
'file-id': {
|
|
12
|
+
type: 'string',
|
|
13
|
+
default: '',
|
|
14
|
+
required: false,
|
|
15
|
+
description: 'The ID of the file or shared drive.'
|
|
16
|
+
},
|
|
11
17
|
'parent-id': {
|
|
12
18
|
type: 'string',
|
|
13
|
-
|
|
19
|
+
default: '',
|
|
20
|
+
required: false,
|
|
21
|
+
description: 'The IDs of the parent folders which contain the file.'
|
|
14
22
|
},
|
|
15
23
|
'dest-file-name': {
|
|
16
24
|
type: 'string',
|
|
17
|
-
|
|
25
|
+
default: '',
|
|
26
|
+
required: false,
|
|
27
|
+
description: 'The name of the file in remote'
|
|
18
28
|
},
|
|
19
29
|
'src-file-name': {
|
|
20
30
|
type: 'string',
|
|
21
|
-
|
|
31
|
+
required: true,
|
|
32
|
+
description: 'The name(path) of the file in local filesystem'
|
|
33
|
+
},
|
|
34
|
+
'dest-mime-type': {
|
|
35
|
+
type: 'string',
|
|
36
|
+
required: false,
|
|
37
|
+
default: '',
|
|
38
|
+
description: 'The MIME type of the file.'
|
|
39
|
+
},
|
|
40
|
+
'src-mime-type': {
|
|
41
|
+
type: 'string',
|
|
42
|
+
required: false,
|
|
43
|
+
default: '',
|
|
44
|
+
description: 'Media mime-type'
|
|
45
|
+
},
|
|
46
|
+
'print-id': {
|
|
47
|
+
type: 'boolean',
|
|
48
|
+
required: false,
|
|
49
|
+
default: false,
|
|
50
|
+
description: 'Print the id of the file that is sended into remote'
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
})
|
|
54
|
+
.command('share [OPTIONS]', 'share file in Google Drive', (yargs) => {
|
|
55
|
+
return yargs.options({
|
|
56
|
+
'file-id': {
|
|
57
|
+
type: 'string',
|
|
58
|
+
default: '',
|
|
59
|
+
required: false,
|
|
60
|
+
description: 'The ID of the file or shared drive.'
|
|
61
|
+
},
|
|
62
|
+
'parent-id': {
|
|
63
|
+
type: 'string',
|
|
64
|
+
default: '',
|
|
65
|
+
required: false,
|
|
66
|
+
description: 'The IDs of the parent folders which contain the file.'
|
|
67
|
+
},
|
|
68
|
+
'dest-file-name': {
|
|
69
|
+
type: 'string',
|
|
70
|
+
default: '',
|
|
71
|
+
required: false,
|
|
72
|
+
description: 'The name of the file in remote'
|
|
73
|
+
},
|
|
74
|
+
type: {
|
|
75
|
+
choices: ['user', 'group', 'domain', 'anyone'],
|
|
76
|
+
required: false,
|
|
77
|
+
default: 'user',
|
|
78
|
+
description: 'The type of the grantee.'
|
|
79
|
+
},
|
|
80
|
+
role: {
|
|
81
|
+
choices: [
|
|
82
|
+
'owner',
|
|
83
|
+
'organizer',
|
|
84
|
+
'fileOrganizer',
|
|
85
|
+
'writer',
|
|
86
|
+
'commenter',
|
|
87
|
+
'reader'
|
|
88
|
+
],
|
|
89
|
+
required: false,
|
|
90
|
+
default: 'reader',
|
|
91
|
+
description: 'The role granted by this permission'
|
|
92
|
+
},
|
|
93
|
+
'email-address': {
|
|
94
|
+
type: 'string',
|
|
95
|
+
default: '',
|
|
96
|
+
description: 'The email address of the user or group to which this permission refers.'
|
|
97
|
+
},
|
|
98
|
+
domain: {
|
|
99
|
+
type: 'string',
|
|
100
|
+
default: '',
|
|
101
|
+
description: 'The domain to which this permission refers. '
|
|
102
|
+
},
|
|
103
|
+
'move-to-new-owners-root': {
|
|
104
|
+
type: 'boolean',
|
|
105
|
+
description: 'This parameter will only take effect if the item is not in a shared drive and the request is attempting to transfer the ownership of the item.'
|
|
106
|
+
},
|
|
107
|
+
'allow-file-discovery': {
|
|
108
|
+
type: 'boolean',
|
|
109
|
+
description: 'Whether the permission allows the file to be discovered through search.'
|
|
110
|
+
},
|
|
111
|
+
view: {
|
|
112
|
+
type: 'string',
|
|
113
|
+
default: '',
|
|
114
|
+
description: 'Whether the permission allows the file to be discovered through search. This is only applicable for permissions of type domain or anyone.'
|
|
115
|
+
},
|
|
116
|
+
'transfer-ownership': {
|
|
117
|
+
type: 'boolean',
|
|
118
|
+
description: 'Whether to transfer ownership to the specified user and downgrade the current owner to a writer.'
|
|
119
|
+
},
|
|
120
|
+
'send-notification-email': {
|
|
121
|
+
type: 'boolean',
|
|
122
|
+
description: 'Whether to send a notification email when sharing to users or groups.'
|
|
123
|
+
},
|
|
124
|
+
'email-message': {
|
|
125
|
+
type: 'string',
|
|
126
|
+
default: '',
|
|
127
|
+
description: 'A plain text custom message to include in the notification email.'
|
|
22
128
|
},
|
|
23
129
|
'print-id': {
|
|
24
130
|
type: 'boolean',
|
|
@@ -28,13 +134,46 @@ const argv = await yargs(hideBin(process.argv))
|
|
|
28
134
|
}
|
|
29
135
|
});
|
|
30
136
|
})
|
|
137
|
+
.demandCommand()
|
|
31
138
|
.demand(0)
|
|
139
|
+
.strictOptions(true)
|
|
32
140
|
.help().argv;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
141
|
+
switch (`${argv._[0]}`) {
|
|
142
|
+
case 'send':
|
|
143
|
+
process.exit(await cliSend({
|
|
144
|
+
fileId: argv['file-id'],
|
|
145
|
+
parentId: argv['parent-id'] || '',
|
|
146
|
+
destFileName: argv['dest-file-name'] || '',
|
|
147
|
+
srcFileName: argv['src-file-name'] || '',
|
|
148
|
+
destMimeType: argv['dest-mime-type'] || '',
|
|
149
|
+
srcMimeType: argv['src-mime-type'] || '',
|
|
150
|
+
printId: argv['print-id'] || false,
|
|
151
|
+
stdout: process.stdout,
|
|
152
|
+
stderr: process.stderr
|
|
153
|
+
}));
|
|
154
|
+
break;
|
|
155
|
+
case 'share':
|
|
156
|
+
process.exit(await cliShare({
|
|
157
|
+
fileId: argv['file-id'],
|
|
158
|
+
parentId: argv['parent-id'] || '',
|
|
159
|
+
destFileName: argv['dest-file-name'] || '',
|
|
160
|
+
type: argv['type'],
|
|
161
|
+
role: argv['role'],
|
|
162
|
+
emailAddress: argv['email-address'],
|
|
163
|
+
domain: argv['domain'],
|
|
164
|
+
view: argv['view'],
|
|
165
|
+
allowFileDiscovery: argv['allow-file-discovery'],
|
|
166
|
+
moveToNewOwnersRoot: argv['move-to-new-owners-root'],
|
|
167
|
+
transferOwnership: argv['transfer-ownership'],
|
|
168
|
+
sendNotificationEmail: argv['send-notification-email'],
|
|
169
|
+
printId: argv['print-id'] || false,
|
|
170
|
+
emailMessage: argv['email-message'],
|
|
171
|
+
stdout: process.stdout,
|
|
172
|
+
stderr: process.stderr
|
|
173
|
+
}));
|
|
174
|
+
break;
|
|
175
|
+
default:
|
|
176
|
+
console.log(argv);
|
|
177
|
+
process.stderr.write(`unknown command: ${argv._[0]}\n`);
|
|
178
|
+
process.exit(1);
|
|
179
|
+
}
|
package/dist/tdrive.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validate value that is used in query parameter.
|
|
3
|
+
* return false if value has included "'">
|
|
4
|
+
* @param s - value string.
|
|
5
|
+
* @returns result of validation.
|
|
6
|
+
*/
|
|
7
|
+
export declare function validateQueryValue(s: string): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Make instacen of drive that is authenticated.
|
|
10
|
+
* @returns instance of drive.
|
|
11
|
+
*/
|
|
12
|
+
export declare function driveClient(): import("@googleapis/drive").drive_v3.Drive;
|
package/dist/tdrive.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { GoogleAuth } from 'google-auth-library';
|
|
2
|
+
import { drive as gdrive } from '@googleapis/drive';
|
|
3
|
+
/**
|
|
4
|
+
* Validate value that is used in query parameter.
|
|
5
|
+
* return false if value has included "'">
|
|
6
|
+
* @param s - value string.
|
|
7
|
+
* @returns result of validation.
|
|
8
|
+
*/
|
|
9
|
+
export function validateQueryValue(s) {
|
|
10
|
+
if (s.indexOf("'") >= 0) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Make instacen of drive that is authenticated.
|
|
17
|
+
* @returns instance of drive.
|
|
18
|
+
*/
|
|
19
|
+
export function driveClient() {
|
|
20
|
+
const SCOPES = ['https://www.googleapis.com/auth/drive.file'];
|
|
21
|
+
const auth = new GoogleAuth({
|
|
22
|
+
scopes: SCOPES
|
|
23
|
+
});
|
|
24
|
+
return gdrive({ version: 'v3', auth });
|
|
25
|
+
}
|
package/dist/tsend.d.ts
CHANGED
|
@@ -9,17 +9,35 @@ export declare class UpdateFileError extends Error {
|
|
|
9
9
|
constructor(message: string);
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
* return false if value has included "'">
|
|
14
|
-
* @param s - value string.
|
|
15
|
-
* @returns result of validation.
|
|
12
|
+
* Options for sendFile().
|
|
16
13
|
*/
|
|
17
|
-
export declare
|
|
18
|
-
/**
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
14
|
+
export declare type SendFileOpts = {
|
|
15
|
+
/**
|
|
16
|
+
* @type The ID of the file or shared drive.
|
|
17
|
+
*/
|
|
18
|
+
fileId: string;
|
|
19
|
+
/**
|
|
20
|
+
/**
|
|
21
|
+
* @type The IDs of the parent folders which contain the file.
|
|
22
|
+
*/
|
|
23
|
+
parentId: string;
|
|
24
|
+
/**
|
|
25
|
+
* @type The name of the file in remote
|
|
26
|
+
*/
|
|
27
|
+
destFileName: string;
|
|
28
|
+
/**
|
|
29
|
+
@type The name(path) of the file in local filesystem
|
|
30
|
+
*/
|
|
31
|
+
srcFileName: string;
|
|
32
|
+
/**
|
|
33
|
+
* @type The MIME type of the file.
|
|
34
|
+
*/
|
|
35
|
+
destMimeType: string;
|
|
36
|
+
/**
|
|
37
|
+
* @type Media mime-type.
|
|
38
|
+
*/
|
|
39
|
+
srcMimeType: string;
|
|
40
|
+
};
|
|
23
41
|
/**
|
|
24
42
|
* Get file id in spesiced parent.
|
|
25
43
|
* @param drive - drive instance.
|
|
@@ -31,18 +49,23 @@ export declare function getFileId(drive: drive_v3.Drive, parentId: string, fileN
|
|
|
31
49
|
/**
|
|
32
50
|
* Create file using by source file into Google Drive.
|
|
33
51
|
* @param drive - drive instance.
|
|
34
|
-
* @param
|
|
35
|
-
* @
|
|
36
|
-
* @param srcFileName - file name in local filesystem.
|
|
37
|
-
* @returns id of file in Google Drive
|
|
52
|
+
* @param opts - options.
|
|
53
|
+
* @returns Print the id of the file that is sended into remote
|
|
38
54
|
*/
|
|
39
|
-
export declare function uploadFile(drive: drive_v3.Drive,
|
|
55
|
+
export declare function uploadFile(drive: drive_v3.Drive, opts: Pick<SendFileOpts, 'parentId' | 'destFileName' | 'srcFileName' | 'destMimeType' | 'srcMimeType'>): Promise<string>;
|
|
40
56
|
/**
|
|
41
57
|
* Update file using by source file into Google Drive.
|
|
42
58
|
* @param drive - drive instance.
|
|
43
|
-
* @param
|
|
44
|
-
* @
|
|
59
|
+
* @param opts - options.
|
|
60
|
+
* @returns id of file in Google Drive
|
|
61
|
+
*/
|
|
62
|
+
export declare function updateFile(drive: drive_v3.Drive, opts: {
|
|
63
|
+
fileId: string;
|
|
64
|
+
} & Pick<SendFileOpts, 'srcFileName' | 'destMimeType' | 'srcMimeType'>): Promise<string>;
|
|
65
|
+
/**
|
|
66
|
+
* Send file using by source file into Google Drive.
|
|
67
|
+
* @param drive - drive instance.
|
|
68
|
+
* @param opts - options.
|
|
45
69
|
* @returns id of file in Google Drive
|
|
46
70
|
*/
|
|
47
|
-
export declare function
|
|
48
|
-
export declare function sendFile(drive: drive_v3.Drive, parentId: string, destFileName: string, srcFileName: string): Promise<string>;
|
|
71
|
+
export declare function sendFile(drive: drive_v3.Drive, opts: SendFileOpts): Promise<string>;
|
package/dist/tsend.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import * as path from 'path';
|
|
2
2
|
import * as fs from 'fs';
|
|
3
|
-
import {
|
|
4
|
-
import { GoogleAuth } from 'google-auth-library';
|
|
3
|
+
import { validateQueryValue } from './tdrive.js';
|
|
5
4
|
export class GetFileIdError extends Error {
|
|
6
5
|
constructor(message) {
|
|
7
6
|
//https://stackoverflow.com/questions/41102060/typescript-extending-error-class
|
|
@@ -23,29 +22,6 @@ export class UpdateFileError extends Error {
|
|
|
23
22
|
Object.setPrototypeOf(this, UpdateFileError.prototype);
|
|
24
23
|
}
|
|
25
24
|
}
|
|
26
|
-
/**
|
|
27
|
-
* Validate value that is used in query parameter.
|
|
28
|
-
* return false if value has included "'">
|
|
29
|
-
* @param s - value string.
|
|
30
|
-
* @returns result of validation.
|
|
31
|
-
*/
|
|
32
|
-
export function validateQueryValue(s) {
|
|
33
|
-
if (s.indexOf("'") >= 0) {
|
|
34
|
-
return false;
|
|
35
|
-
}
|
|
36
|
-
return true;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Make instacen of drive that is authenticated.
|
|
40
|
-
* @returns instance of drive.
|
|
41
|
-
*/
|
|
42
|
-
export function driveClient() {
|
|
43
|
-
const SCOPES = ['https://www.googleapis.com/auth/drive.file'];
|
|
44
|
-
const auth = new GoogleAuth({
|
|
45
|
-
scopes: SCOPES
|
|
46
|
-
});
|
|
47
|
-
return gdrive({ version: 'v3', auth });
|
|
48
|
-
}
|
|
49
25
|
/**
|
|
50
26
|
* Get file id in spesiced parent.
|
|
51
27
|
* @param drive - drive instance.
|
|
@@ -81,24 +57,29 @@ export async function getFileId(drive, parentId, fileName) {
|
|
|
81
57
|
/**
|
|
82
58
|
* Create file using by source file into Google Drive.
|
|
83
59
|
* @param drive - drive instance.
|
|
84
|
-
* @param
|
|
85
|
-
* @
|
|
86
|
-
* @param srcFileName - file name in local filesystem.
|
|
87
|
-
* @returns id of file in Google Drive
|
|
60
|
+
* @param opts - options.
|
|
61
|
+
* @returns Print the id of the file that is sended into remote
|
|
88
62
|
*/
|
|
89
|
-
export async function uploadFile(drive,
|
|
63
|
+
export async function uploadFile(drive, opts) {
|
|
90
64
|
try {
|
|
91
|
-
const
|
|
65
|
+
const { parentId, destFileName, srcFileName, destMimeType, srcMimeType } = opts;
|
|
66
|
+
const params = {
|
|
92
67
|
requestBody: {
|
|
93
68
|
name: path.basename(destFileName),
|
|
94
69
|
parents: [parentId]
|
|
95
70
|
},
|
|
96
71
|
media: {
|
|
97
|
-
// mimeType: 'image/jpeg',
|
|
98
72
|
body: fs.createReadStream(srcFileName)
|
|
99
73
|
},
|
|
100
74
|
fields: 'id'
|
|
101
|
-
}
|
|
75
|
+
};
|
|
76
|
+
if (destMimeType) {
|
|
77
|
+
params.requestBody.mimeType = destMimeType;
|
|
78
|
+
}
|
|
79
|
+
if (srcMimeType) {
|
|
80
|
+
params.media.mimeType = srcMimeType;
|
|
81
|
+
}
|
|
82
|
+
const res = await drive.files.create(params);
|
|
102
83
|
return res.data.id || '';
|
|
103
84
|
}
|
|
104
85
|
catch (err) {
|
|
@@ -108,43 +89,50 @@ export async function uploadFile(drive, parentId, destFileName, srcFileName) {
|
|
|
108
89
|
/**
|
|
109
90
|
* Update file using by source file into Google Drive.
|
|
110
91
|
* @param drive - drive instance.
|
|
111
|
-
* @param
|
|
112
|
-
* @param srcFileName - file name in local filesystem.
|
|
92
|
+
* @param opts - options.
|
|
113
93
|
* @returns id of file in Google Drive
|
|
114
94
|
*/
|
|
115
|
-
export async function updateFile(drive,
|
|
95
|
+
export async function updateFile(drive, opts) {
|
|
116
96
|
try {
|
|
117
|
-
const
|
|
97
|
+
const { fileId, srcFileName, destMimeType, srcMimeType } = opts;
|
|
98
|
+
const params = {
|
|
118
99
|
fileId,
|
|
119
100
|
requestBody: {},
|
|
120
101
|
media: {
|
|
121
|
-
// mimeType: 'image/jpeg',
|
|
122
102
|
body: fs.createReadStream(srcFileName)
|
|
123
103
|
},
|
|
124
104
|
fields: 'id'
|
|
125
|
-
}
|
|
105
|
+
};
|
|
106
|
+
if (destMimeType) {
|
|
107
|
+
params.requestBody.mimeType = destMimeType;
|
|
108
|
+
}
|
|
109
|
+
if (srcMimeType) {
|
|
110
|
+
params.media.mimeType = srcMimeType;
|
|
111
|
+
}
|
|
112
|
+
const res = await drive.files.update(params);
|
|
126
113
|
return res.data.id || '';
|
|
127
114
|
}
|
|
128
115
|
catch (err) {
|
|
129
116
|
throw new UpdateFileError(JSON.stringify(err.errors));
|
|
130
117
|
}
|
|
131
118
|
}
|
|
132
|
-
|
|
133
|
-
|
|
119
|
+
/**
|
|
120
|
+
* Send file using by source file into Google Drive.
|
|
121
|
+
* @param drive - drive instance.
|
|
122
|
+
* @param opts - options.
|
|
123
|
+
* @returns id of file in Google Drive
|
|
124
|
+
*/
|
|
125
|
+
export async function sendFile(drive, opts) {
|
|
126
|
+
const { fileId: inFileId, parentId, destFileName, srcFileName, destMimeType, srcMimeType } = opts;
|
|
127
|
+
let fileId = inFileId !== '' ? inFileId : await getFileId(drive, parentId, destFileName);
|
|
134
128
|
if (fileId === '') {
|
|
135
|
-
return uploadFile(drive,
|
|
129
|
+
return uploadFile(drive, {
|
|
130
|
+
parentId,
|
|
131
|
+
destFileName,
|
|
132
|
+
srcFileName,
|
|
133
|
+
destMimeType,
|
|
134
|
+
srcMimeType
|
|
135
|
+
});
|
|
136
136
|
}
|
|
137
|
-
return updateFile(drive, fileId, srcFileName);
|
|
137
|
+
return updateFile(drive, { fileId, srcFileName, destMimeType, srcMimeType });
|
|
138
138
|
}
|
|
139
|
-
// try {
|
|
140
|
-
// const parentId = process.env['PARENT_ID'] || ''
|
|
141
|
-
// const destFileName = process.env['DEST_FILE_NAME'] || ''
|
|
142
|
-
// const srcFileName = process.env['SRC_FILE_NAME'] || ''
|
|
143
|
-
// const drive = driveClient()
|
|
144
|
-
// const id = await getFileId(drive, parentId, destFileName)
|
|
145
|
-
// console.log(id)
|
|
146
|
-
// console.log(await updateFile(drive, id, srcFileName))
|
|
147
|
-
// } catch (err) {
|
|
148
|
-
// console.error('--err--')
|
|
149
|
-
// console.error(err)
|
|
150
|
-
// }
|
package/dist/tshare.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { drive_v3 } from '@googleapis/drive';
|
|
2
|
+
export declare class CreatePermissonError extends Error {
|
|
3
|
+
constructor(message: string);
|
|
4
|
+
}
|
|
5
|
+
export declare class UpdatePermissonError extends Error {
|
|
6
|
+
constructor(message: string);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Options for createPermisson().
|
|
10
|
+
*/
|
|
11
|
+
export declare type CreatePermissonOpts = {
|
|
12
|
+
/**
|
|
13
|
+
* @type The ID of the file or shared drive.
|
|
14
|
+
*/
|
|
15
|
+
fileId: string;
|
|
16
|
+
/**
|
|
17
|
+
* @type The IDs of the parent folders which contain the file.
|
|
18
|
+
*/
|
|
19
|
+
parentId: string;
|
|
20
|
+
/**
|
|
21
|
+
* @type The name of the file in remote
|
|
22
|
+
*/
|
|
23
|
+
destFileName: string;
|
|
24
|
+
/**
|
|
25
|
+
* @type The type of the grantee.
|
|
26
|
+
*/
|
|
27
|
+
type: string;
|
|
28
|
+
/**
|
|
29
|
+
* The role granted by this permission
|
|
30
|
+
*/
|
|
31
|
+
role: string;
|
|
32
|
+
/**
|
|
33
|
+
* @type The email address of the user or group to which this permission refers.
|
|
34
|
+
*/
|
|
35
|
+
emailAddress: string;
|
|
36
|
+
/**
|
|
37
|
+
* @type The domain to which this permission refers.
|
|
38
|
+
*/
|
|
39
|
+
domain: string;
|
|
40
|
+
/**
|
|
41
|
+
* @type Whether the permission allows the file to be discovered through search.
|
|
42
|
+
*/
|
|
43
|
+
allowFileDiscovery?: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* @type Indicates the view for this permission. Only populated for permissions that belong to a view. published is the only supported value.
|
|
46
|
+
*/
|
|
47
|
+
view: string;
|
|
48
|
+
/**
|
|
49
|
+
* @type This parameter will only take effect if the item is not in a shared drive and the request is attempting to transfer the ownership of the item.
|
|
50
|
+
*/
|
|
51
|
+
moveToNewOwnersRoot?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* @type Whether to transfer ownership to the specified user and downgrade the current owner to a writer.
|
|
54
|
+
*/
|
|
55
|
+
transferOwnership?: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* @type Whether the permission allows the file to be discovered through search. This is only applicable for permissions of type domain or anyone.
|
|
58
|
+
*/
|
|
59
|
+
sendNotificationEmail?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* @type A plain text custom message to include in the notification email.
|
|
62
|
+
*/
|
|
63
|
+
emailMessage: string;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Create permission
|
|
67
|
+
* @param drive - drive instance.
|
|
68
|
+
* @param opts
|
|
69
|
+
* @returns id of file in Google Drive
|
|
70
|
+
*/
|
|
71
|
+
export declare function createPermisson(drive: drive_v3.Drive, opts: CreatePermissonOpts): Promise<string>;
|
package/dist/tshare.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { getFileId } from './tsend.js';
|
|
2
|
+
export class CreatePermissonError extends Error {
|
|
3
|
+
constructor(message) {
|
|
4
|
+
//https://stackoverflow.com/questions/41102060/typescript-extending-error-class
|
|
5
|
+
super(message);
|
|
6
|
+
Object.setPrototypeOf(this, CreatePermissonError.prototype);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export class UpdatePermissonError extends Error {
|
|
10
|
+
constructor(message) {
|
|
11
|
+
//https://stackoverflow.com/questions/41102060/typescript-extending-error-class
|
|
12
|
+
super(message);
|
|
13
|
+
Object.setPrototypeOf(this, UpdatePermissonError.prototype);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Create permission
|
|
18
|
+
* @param drive - drive instance.
|
|
19
|
+
* @param opts
|
|
20
|
+
* @returns id of file in Google Drive
|
|
21
|
+
*/
|
|
22
|
+
export async function createPermisson(drive, opts) {
|
|
23
|
+
let created = false;
|
|
24
|
+
try {
|
|
25
|
+
const { fileId: inFileId, parentId, destFileName, type, role, emailAddress, domain, view, allowFileDiscovery, moveToNewOwnersRoot, transferOwnership, sendNotificationEmail, emailMessage } = opts;
|
|
26
|
+
const fileId = inFileId || (await getFileId(drive, parentId, destFileName));
|
|
27
|
+
const createParams = {
|
|
28
|
+
requestBody: {
|
|
29
|
+
type,
|
|
30
|
+
role
|
|
31
|
+
},
|
|
32
|
+
fileId,
|
|
33
|
+
fields: 'id'
|
|
34
|
+
};
|
|
35
|
+
if (emailAddress) {
|
|
36
|
+
createParams.requestBody.emailAddress = emailAddress;
|
|
37
|
+
}
|
|
38
|
+
if (domain) {
|
|
39
|
+
createParams.requestBody.domain = domain;
|
|
40
|
+
}
|
|
41
|
+
if (view) {
|
|
42
|
+
createParams.requestBody.view = view;
|
|
43
|
+
}
|
|
44
|
+
if (allowFileDiscovery !== undefined) {
|
|
45
|
+
createParams.requestBody.allowFileDiscovery = allowFileDiscovery;
|
|
46
|
+
}
|
|
47
|
+
if (moveToNewOwnersRoot !== undefined) {
|
|
48
|
+
createParams.moveToNewOwnersRoot = moveToNewOwnersRoot;
|
|
49
|
+
}
|
|
50
|
+
if (transferOwnership !== undefined) {
|
|
51
|
+
createParams.transferOwnership = transferOwnership;
|
|
52
|
+
}
|
|
53
|
+
if (sendNotificationEmail !== undefined) {
|
|
54
|
+
createParams.sendNotificationEmail = sendNotificationEmail;
|
|
55
|
+
}
|
|
56
|
+
if (sendNotificationEmail && emailMessage) {
|
|
57
|
+
createParams.emailMessage = emailMessage;
|
|
58
|
+
}
|
|
59
|
+
const resCreate = await drive.permissions.create(createParams);
|
|
60
|
+
created = true;
|
|
61
|
+
const id = resCreate.data.id || '';
|
|
62
|
+
if (id === '') {
|
|
63
|
+
throw new CreatePermissonError('drive.permissions.create() return blank id ');
|
|
64
|
+
}
|
|
65
|
+
if (!transferOwnership) {
|
|
66
|
+
const resUpdate = await drive.permissions.update({
|
|
67
|
+
permissionId: id,
|
|
68
|
+
requestBody: {
|
|
69
|
+
role
|
|
70
|
+
},
|
|
71
|
+
fileId,
|
|
72
|
+
fields: 'id'
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return id;
|
|
76
|
+
}
|
|
77
|
+
catch (err) {
|
|
78
|
+
if (err.errors) {
|
|
79
|
+
if (!created) {
|
|
80
|
+
throw new CreatePermissonError(JSON.stringify(err.errors));
|
|
81
|
+
}
|
|
82
|
+
throw new UpdatePermissonError(JSON.stringify(err.errors));
|
|
83
|
+
}
|
|
84
|
+
throw err;
|
|
85
|
+
}
|
|
86
|
+
}
|
package/package.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "guratan",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Tiny send tool for Google Drive",
|
|
5
5
|
"author": "hankei6km <hankei6km@gmail.com> (https://github.com/hankei6km)",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
|
-
"url": "git://github.com/hankei6km/
|
|
9
|
+
"url": "git://github.com/hankei6km/guratan.git"
|
|
10
10
|
},
|
|
11
11
|
"bugs": {
|
|
12
|
-
"url": "https://github.com/hankei6km/
|
|
12
|
+
"url": "https://github.com/hankei6km/guratan/issues"
|
|
13
13
|
},
|
|
14
14
|
"keywords": [
|
|
15
15
|
"google",
|