Skip to content

Commit

Permalink
init: created all files
Browse files Browse the repository at this point in the history
auth, init folders, sync command and ribbonButton added [beta]
  • Loading branch information
stravo1 committed Oct 19, 2022
0 parents commit e75730d
Show file tree
Hide file tree
Showing 11 changed files with 979 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# top-most EditorConfig file
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = tab
indent_size = 4
tab_width = 4
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
npm node_modules
build
23 changes: 23 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"env": { "node": true },
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error", { "args": "none" }],
"@typescript-eslint/ban-ts-comment": "off",
"no-prototype-builtins": "off",
"@typescript-eslint/no-empty-function": "off"
}
}
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# vscode
.vscode

# Intellij
*.iml
.idea

# npm
node_modules

# Don't include the compiled main.js file in the repo.
# They should be uploaded to GitHub releases instead.
main.js

# Exclude sourcemaps
*.map

# obsidian
data.json

# Exclude macOS Finder (System Explorer) View States
.DS_Store
172 changes: 172 additions & 0 deletions actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import { requestUrl } from "obsidian";

const getVaultId = async (accessToken, vault, root = null) => {
const response = await requestUrl({
url:
"https://www.googleapis.com/drive/v3/files?q=mimeType%20%3D%20'application%2Fvnd.google-apps.folder'" +
(root != null ? `%20and%20'${root}'%20in%20parents` : ""),
method: "GET",
headers: {
Authorization: `Bearer ${accessToken}`,
},
}).catch((e) => console.log(e));
const list = response.json.files;
var vaultFolder = list.filter((file) => file.name == vault);
var vaultId = vaultFolder.length ? vaultFolder[0].id : "NOT FOUND";
return vaultId;
};

const uploadFile = async (
accessToken,
fileName,
buffer = null,
parentId = null
) => {
const response = await requestUrl({
url: "https://www.googleapis.com/drive/v3/files?uploadType=multipart",
method: "POST",
headers: {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
},

body: JSON.stringify({
//mimeType: "text/pain",
name: fileName,
parents: parentId ? [parentId] : [],
}),
}).catch((e) => console.log(e));
var id = response.json.id;
if (buffer) {
// upload the metadata
await requestUrl({
url: `https://www.googleapis.com/upload/drive/v3/files/${id}`,
method: "PATCH",
headers: {
Authorization: `Bearer ${accessToken}`,
},

body: buffer,
}).catch((e) => console.log(e));
}
return id;
};

const modifyFile = async (accessToken, fileId, buffer) => {
var res = await requestUrl({
url: `https://www.googleapis.com/upload/drive/v3/files/${fileId}`,
method: "PATCH",
headers: {
Authorization: `Bearer ${accessToken}`,
},

body: buffer,
}).catch((e) => console.log(e));
return res;
};
const renameFile = async (accessToken, fileId, newName) => {
const response = await requestUrl({
url: `https://www.googleapis.com/drive/v3/files/${fileId}`,
method: "PATCH",
headers: {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
},

body: JSON.stringify({
name: newName,
}),
}).catch((e) => console.log(e));
var id = response.json.id;
return id;
};

const deleteFile = async (accessToken, fileId) => {
const response = await requestUrl({
url: `https://www.googleapis.com/drive/v3/files/${fileId}`,
method: "DELETE",
headers: {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
},
}).catch((e) => console.log(e));
};

const uploadFolder = async (accessToken, foldername, rootId = null) => {
const response = await requestUrl({
url: "https://www.googleapis.com/drive/v3/files?uploadType=multipart",
method: "POST",
headers: {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
},

body: JSON.stringify({
mimeType: "application/vnd.google-apps.folder",
name: foldername,
parents: rootId ? [rootId] : [],
}),
}).catch((e) => console.log(e));

var id = response.json.id;
return id;
};

const getFilesList = async (accessToken, vault) => {
const response = await requestUrl({
url:
"https://www.googleapis.com/drive/v3/files" +
(vault != null ? `?q='${vault}'%20in%20parents` : ""),
method: "GET",
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
return response.json.files;
};

const getFoldersList = async (accessToken, vault = null) => {
const response = await requestUrl({
url:
"https://www.googleapis.com/drive/v3/files?q=mimeType%20%3D%20'application%2Fvnd.google-apps.folder'" +
(vault != null ? `%20and%20'${vault}'%20in%20parents` : ""),
method: "GET",
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
return response.json.files;
};
const getFile = async (accessToken, fileId) => {
const responseBuffer = await requestUrl({
url:
"https://www.googleapis.com/drive/v3/files/" +
fileId +
"?alt=media",

method: "GET",
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
const responseName = await requestUrl({
url: "https://www.googleapis.com/drive/v3/files/" + fileId,

method: "GET",
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
return [responseName.json.name, responseBuffer.arrayBuffer];
};
export {
getVaultId,
getFilesList,
getFoldersList,
uploadFile,
uploadFolder,
getFile,
renameFile,
deleteFile,
modifyFile,
};
42 changes: 42 additions & 0 deletions esbuild.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import esbuild from "esbuild";
import process from "process";
import builtins from 'builtin-modules'

const banner =
`/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
`;

const prod = (process.argv[2] === 'production');

esbuild.build({
banner: {
js: banner,
},
entryPoints: ['main.ts'],
bundle: true,
external: [
'obsidian',
'electron',
'@codemirror/autocomplete',
'@codemirror/collab',
'@codemirror/commands',
'@codemirror/language',
'@codemirror/lint',
'@codemirror/search',
'@codemirror/state',
'@codemirror/view',
'@lezer/common',
'@lezer/highlight',
'@lezer/lr',
...builtins],
format: 'cjs',
watch: !prod,
target: 'es2018',
logLevel: "info",
sourcemap: prod ? false : 'inline',
treeShaking: true,
outfile: 'main.js',
}).catch(() => process.exit(1));
Loading

0 comments on commit e75730d

Please sign in to comment.