Skip to content

Commit

Permalink
feat: ✨ support AList
Browse files Browse the repository at this point in the history
  • Loading branch information
yaleiyale committed Nov 22, 2023
1 parent 34e44d2 commit c6b2776
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 68 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ This plugin is for uploading **images** to hosting platform or **files** to Gith

## State

| file hosting | image hosting | Multi language support |
| ------------------------------------ | --------------------------------- | --------------------------------------------------- |
| [GitHub](https://github.com/) | [Imgur](https://imgur.com/) | 简体中文 |
| [Clouinary](https://cloudinary.com/) | [SM.MS](https://smms.app/) | 繁體中文 [@emisjerry](https://github.com/emisjerry) |
| [Catbox](https://catbox.moe/) | [ImgURL](https://www.imgurl.org/) | English |
| | [imgbb](https://imgbb.com/) | |
| | [chevereto](https://chevereto.com/) | |
| file hosting | image hosting | Multi language support |
| ------------------------------------ | ----------------------------------- | --------------------------------------------------- |
| [GitHub](https://github.com/) | [Imgur](https://imgur.com/) | 简体中文 |
| [Clouinary](https://cloudinary.com/) | [SM.MS](https://smms.app/) | 繁體中文 [@emisjerry](https://github.com/emisjerry) |
| [Catbox](https://catbox.moe/) | [ImgURL](https://www.imgurl.org/) | English |
| [AList](https://alist.nn.ci/zh/) | [imgbb](https://imgbb.com/) | |
| | [chevereto](https://chevereto.com/) | |

### 2.17

support [AList](https://alist.nn.ci/zh/), thanks to [Linnnkkk](https://github.com/Linnnkkk).

### V2.16

Adapted to chevereto v3


### V2.15

support [chevereto](https://chevereto.com/)
Expand Down Expand Up @@ -141,4 +144,4 @@ Thank you!
| [obsidian-cloudinary-uploader](https://github.com/jordanhandy/obsidian-cloudinary-uploader) | [Github REST API](https://docs.github.com/cn/rest) | [jsdelivr](https://www.jsdelivr.com/) |
| [SM.MS](https://smms.app/) | [ImgURL](https://www.imgurl.org/) | [Clouinary](https://cloudinary.com/) |
| [Imgur](https://imgur.com/) | [imgbb](https://imgbb.com/) | [obsidian-imgur-plugin](https://github.com/gavvvr/obsidian-imgur-plugin) |
| [Catbox](https://catbox.moe/) | [chevereto](https://chevereto.com/) | |
| [Catbox](https://catbox.moe/) | [chevereto](https://chevereto.com/) | [AList](https://alist.nn.ci/zh/) |
Binary file removed docs/assets/obsidian-settings-page.png
Binary file not shown.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "emo-uploader",
"name": "Emo",
"version": "2.16",
"version": "2.17",
"minAppVersion": "0.15.6",
"description": "Embed markdown online file/image links. This plugin is for uploading images to hosting or files to github in Obsidian.",
"author": "yaleiyale",
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"keywords": [],
"author": "yaleiyale",
"license": "MIT",
"dependencies": {
"js-sha256": "^0.10.1"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.61.0",
"eslint": "^8.44.0",
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export enum HostingProvider { // target hosting
ImgURL = 'ImgURL',
Imgbb = 'imgbb',
Chevereto = 'chevereto',
Alist = 'alist'
Alist = 'AList'
}

export const DEFAULT_SETTINGS: Config = {
Expand Down
10 changes: 5 additions & 5 deletions src/fragment/fragment-alist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ export class AlistFragment extends EmoFragment {

display (el: HTMLElement, plugin: Emo): void {
const parms = plugin.config.alist_parms
el.createEl('h3', { text: 'Alist Settings'})
el.createEl('h3', { text: t('AList Settings') })

new Setting(el)
.setName(t('domain'))
.addText((text) => {
text
.setPlaceholder('https://alist.example.com')
.setValue(parms.required.domain)
.onChange(async (value) => {
parms.required.domain = value
Expand All @@ -25,7 +26,7 @@ export class AlistFragment extends EmoFragment {
})

new Setting(el)
.setName('username')
.setName(t('username'))
.addText((text) => {
text
.setValue(parms.required.username)
Expand All @@ -36,7 +37,7 @@ export class AlistFragment extends EmoFragment {
})

new Setting(el)
.setName('password')
.setName(t('password'))
.addText((text) => {
text
.setValue(parms.required.password)
Expand All @@ -47,7 +48,7 @@ export class AlistFragment extends EmoFragment {
})

new Setting(el)
.setName('uploadPath')
.setName(t('uploadPath'))
.addText((text) => {
text
.setValue(parms.required.uploadPath)
Expand All @@ -56,6 +57,5 @@ export class AlistFragment extends EmoFragment {
await plugin.saveSettings()
})
})

}
}
7 changes: 6 additions & 1 deletion src/lang/locale/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,10 @@ export default {
// fragment-catbox.ts
'Catbox Settings': 'Catbox Settings',
// fragment-chevereto.ts
'Chevereto Settings': 'Chevereto Settings'
'Chevereto Settings': 'Chevereto Settings',
// fragment-alist.ts
'AList Settings': 'Alist Settings',
username: 'username',
password: 'password',
uploadPath: 'uploadPath'
}
7 changes: 6 additions & 1 deletion src/lang/locale/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,10 @@ export default {
// fragment-catbox.ts
'Catbox Settings': 'Catbox配置',
// fragment-chevereto.ts
'Chevereto Settings': 'Chevereto配置'
'Chevereto Settings': 'Chevereto配置',
// fragment-alist.ts
'AList Settings': 'Alist配置',
username: '用户名',
password: '密码',
uploadPath: '上传路径'
}
7 changes: 6 additions & 1 deletion src/lang/locale/zh-tw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,10 @@ export default {
// fragment-catbox.ts
'Catbox Settings': 'Catbox配置',
// fragment-chevereto.ts
'Chevereto Settings': 'Chevereto配置'
'Chevereto Settings': 'Chevereto配置',
// fragment-alist.ts
'AList Settings': 'Alist配置',
username: '用戶名',
password: '密碼',
uploadPath: '上傳路徑'
}
5 changes: 2 additions & 3 deletions src/parms/parms-alist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ interface Required {

export const ALIST_DEFAULT_PARMS: AlistParms = {
required: {
domain: 'https://alist.example.com',
domain: '',
username: '',
password: '',
uploadPath: '上传的相对路径',
uploadPath: ''
}
}

85 changes: 39 additions & 46 deletions src/uploader/uploader-alist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,31 @@ export class AlistUploader extends EmoUploader {
this.parms = alistParms
}


async upload (file: File): Promise<string> {
// 获取token
const token = await this.getToken()
// 新建文件夹
const determine = await this.determine(file)
if (determine != 'success') {
await this.mkdirFile(file)
if (determine !== 'success') {
await this.mkdirFile(file)
}
// 上传文件
await this.putFile(file)
// 刷新列表
await this.refreshDir(file)
// 重命名文件
let newName = file.name
if (file.name == 'image.png'){
newName = await this.renameFile(file)
if (file.name === 'image.png') {
newName = await this.renameFile(file)
}

const extension = await this.getFileExtension(file)
const req: RequestUrlParam = {
url: `${this.parms.required.domain}/api/fs/get`,
method: 'POST',
headers: {
'Authorization': token ,
'Content-Type': 'application/json',
Authorization: token,
'Content-Type': 'application/json'
},
body: JSON.stringify({
path: `/${this.parms.required.uploadPath}/${extension}/${newName}`
Expand All @@ -49,16 +48,16 @@ export class AlistUploader extends EmoUploader {
request(req).then(async res => {
const json = JSON.parse(res)
// 赋给markdownText
const markdownText = `![${newName}](${json.data.raw_url})`
const markdownText = `![${newName}](${json.data.raw_url})`
resolve(markdownText)
}).catch(err => {
reject(err)
})
})
}

//第二部分
//获取token
// 第二部分
// 获取token
async getToken (): Promise<string> {
const req: RequestUrlParam = {
url: `${this.parms.required.domain}/api/auth/login/hash`,
Expand All @@ -82,23 +81,22 @@ export class AlistUploader extends EmoUploader {
})
}


//文件类型
async getFileExtension(file: File): Promise<string> {
// 文件类型
async getFileExtension (file: File): Promise<string> {
const filename = file.name
const match = filename.match(/\.([^.]+)$/)
return match ? match[1] : ""
return (match != null) ? match[1] : ''
}

//判断文件夹是否存在
// 判断文件夹是否存在
async determine (file: File): Promise<string> {
const extension = await this.getFileExtension(file)
const req: RequestUrlParam = {
url: `${this.parms.required.domain}/api/fs/get`,
method: 'POST',
headers: {
'Authorization': await this.getToken(),
'Content-Type': 'application/json',
Authorization: await this.getToken(),
'Content-Type': 'application/json'
},
body: JSON.stringify({
path: `/${this.parms.required.uploadPath}/${extension}`
Expand All @@ -114,23 +112,21 @@ export class AlistUploader extends EmoUploader {
})
}

async mkdirFile (file: File): Promise<void> {
const extension = await this.getFileExtension(file)
const req: RequestUrlParam = {
url: `${this.parms.required.domain}/api/fs/mkdir`,
method: 'POST',
headers: {
'Authorization': await this.getToken(),
'Content-Type': 'application/json',
},
body: JSON.stringify({
path: `/${this.parms.required.uploadPath}/${extension}`,
})
}
await request(req) // 发送请求并返回链接
}


async mkdirFile (file: File): Promise<void> {
const extension = await this.getFileExtension(file)
const req: RequestUrlParam = {
url: `${this.parms.required.domain}/api/fs/mkdir`,
method: 'POST',
headers: {
Authorization: await this.getToken(),
'Content-Type': 'application/json'
},
body: JSON.stringify({
path: `/${this.parms.required.uploadPath}/${extension}`
})
}
await request(req) // 发送请求并返回链接
}

// 上传文件
async putFile (file: File): Promise<void> {
Expand All @@ -141,7 +137,7 @@ export class AlistUploader extends EmoUploader {
url: `${this.parms.required.domain}/api/fs/form`,
method: 'PUT',
headers: {
'Authorization': await this.getToken(),
Authorization: await this.getToken(),
'Content-Type': CONTENT_TYPE_FORMDATA,
'File-Path': encodeURIComponent(`/${this.parms.required.uploadPath}/${extension}/${file.name}`),
'As-Task': 'true'
Expand All @@ -151,27 +147,26 @@ export class AlistUploader extends EmoUploader {
await request(req) // 发送请求并返回链接
}

//第三部分
// 第三部分
// 刷新目录
async refreshDir (file: File): Promise<void> {
const extension = await this.getFileExtension(file)
const req: RequestUrlParam = {
url: `${this.parms.required.domain}/api/fs/list`,
method: 'POST',
headers: {
'Authorization': await this.getToken(),
'Content-Type': 'application/json',
Authorization: await this.getToken(),
'Content-Type': 'application/json'
},
body: JSON.stringify({
path: `/${this.parms.required.uploadPath}/${extension}`,
page: 1,
password: "",
password: '',
per_page: 0,
refresh: true,
refresh: true
})
}
await request(req)

}

// 重命名文件
Expand All @@ -188,8 +183,8 @@ export class AlistUploader extends EmoUploader {
url: `${this.parms.required.domain}/api/fs/rename`,
method: 'POST',
headers: {
'Authorization': await this.getToken(),
'Content-Type': 'application/json',
Authorization: await this.getToken(),
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: newName,
Expand All @@ -200,6 +195,4 @@ export class AlistUploader extends EmoUploader {
await request(req)
return newName
}

}

0 comments on commit c6b2776

Please sign in to comment.