-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclam.js
More file actions
85 lines (78 loc) · 2.98 KB
/
clam.js
File metadata and controls
85 lines (78 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* clam
* Copyright(c) 2022 Hacknock
* Author: Akira Kashihara <akira.kashihara@hotmail.com>
* MIT Licensed, The detail is on README.md and LICENSE.
*/
import GCS from "./lib/gcs.js";
import ToolKit from "./lib/toolkit.js";
class Clam {
/**
* Clam class is an entry point to your application or service.
* @param {string} [name="GCS"] - Put "GCS" if you use Google Cloud Storage
*/
constructor(name = "GCS") {
this.name = name;
console.log("make Clam instance mode is " + name);
if (name === "GCS") {
this.module = new GCS(new ToolKit());
} else {
throw Error(
"The second argument is invalid. You can set only 'GCS'. It means that you use Google Cloud Storage."
);
}
}
/**
* setCred is a method to set the credential information.
* @param {JSON} params - Setting parameter using authorization. This must include 'clientId', 'redirectUrl' and 'scope'
* @param {string} params.clientId - You get client ID from Google Cloud Platform API / Credentials.
* @param {string} params.redirectUrl - Redirect URL. It will get autorized information from Google.
* @param {string} params.scope - Scope is access permission level. Please refer to https://developers.google.com/identity/protocols/oauth2/scopes
*/
setCred = (params) => {
if (!params) throw Error("You must set the first argument.");
if (params.clientId && params.redirectUrl && params.scope) {
try {
this.module.setCred(params);
} catch (err) {
throw err;
}
} else {
throw Error(
"The first argument is invalid. You must set 'clientId', 'redirectUrl' and 'scope'."
);
}
};
/**
* This function provide auth page to each storage service.
*/
toAuthPage = () => {
console.log("Auth");
if (!this.module) throw Error("This instance is not initialzed.");
this.module.oauthSignIn();
};
/**
* This function gets the information to access to storage service from redirect URL.
* @returns {JSON} Return value is JSON which includes the information to access to storage service.
*/
getAuthInfo = () => {
console.log("Auth info + " + this.name);
if (!this.module) throw Error("This instance is not initialzed.");
if (this.name === "GCS") {
return this.module.getAccessToken(location);
}
};
/**
* This function uploads files you selected to storage service.
* @param {Object} fileList - fileList is from <input type="file" multiple>
* @param {string} bucketName - The bucket name you will upload file to.
* @param {() => void} callback - The callback function. 1st argument is an error. 2nd argument is file name.
* @returns {JSON} - Return JSON includes file name list of each result; success or failed.
*/
uploadFiles = async (fileList, bucketName, callback) => {
console.log("Uplad");
if (!this.module) throw Error("This instance is not initialzed.");
return this.module.uploadFiles(fileList, bucketName, callback);
};
}
export default Clam;