-
Notifications
You must be signed in to change notification settings - Fork 18
Add year-month segmented folders for the Github image hosting. #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import Octokit from '@octokit/rest' | ||
| import dayjs from 'dayjs' | ||
| import { getNow, pathJoin } from './helper' | ||
| import { PluginConfig, ImgType } from './interface' | ||
| import urlJoin from 'url-join' | ||
|
|
@@ -15,13 +16,15 @@ export class Octo { | |
| customUrl: string = '' | ||
| octokit: Octokit = null | ||
| origin: PluginConfig['origin'] | ||
| pathByDate: boolean = false | ||
| constructor ({ | ||
| repo, | ||
| branch, | ||
| path = '', | ||
| token, | ||
| customUrl = '', | ||
| origin = 'github' | ||
| origin = 'github', | ||
| pathByDate = false | ||
| }: PluginConfig) { | ||
| const [owner, r] = repo.split('/') | ||
| if (!r) throw new Error('Error in repo name') | ||
|
|
@@ -32,6 +35,7 @@ export class Octo { | |
| this.token = token | ||
| this.customUrl = customUrl | ||
| this.origin = origin | ||
| this.pathByDate = pathByDate | ||
| this.octokit = new Octokit({ | ||
| baseUrl: origin === 'github' ? GithubUrl : GiteeUrl, | ||
| auth: token ? `token ${token}` : undefined | ||
|
|
@@ -63,6 +67,29 @@ export class Octo { | |
| } | ||
| return { sha, tree } | ||
| } | ||
| /** Recursively get all files under path (for Pull when pathByDate). Returns flat list with paths relative to this.path. */ | ||
| async getPathTreeRecursive (): Promise<{ tree: { path: string; sha: string }[] }> { | ||
| const { tree } = await this.getPathTree() | ||
| const flat: { path: string; sha: string }[] = [] | ||
| const collect = async (sha: string, prefix: string): Promise<void> => { | ||
| const list = await this.getTree(sha) | ||
| for (const item of list) { | ||
| if (item.type === 'tree') { | ||
| await collect(item.sha, prefix + item.path + '/') | ||
| } else { | ||
| flat.push({ path: prefix + item.path, sha: item.sha }) | ||
| } | ||
| } | ||
| } | ||
| for (const item of tree) { | ||
| if (item.type === 'tree') { | ||
| await collect(item.sha, item.path + '/') | ||
| } else { | ||
| flat.push({ path: item.path, sha: item.sha }) | ||
| } | ||
| } | ||
| return { tree: flat } | ||
| } | ||
| createFile(params) { | ||
| const { isGithub } = this | ||
| const request = this.octokit.request(`/repos/:owner/:repo/contents/:path`, { | ||
|
|
@@ -122,22 +149,27 @@ export class Octo { | |
| content: Buffer.from(JSON.stringify(data)).toString('base64') | ||
| }) | ||
| } | ||
| async upload (img: ImgInfo) { | ||
| async upload (img: ImgInfo): Promise<{ imgUrl: string; sha: string; fileName?: string }> { | ||
| /* istanbul ignore next */ | ||
| const { owner, repo, branch, path = '' } = this | ||
| const { fileName } = img | ||
| const effectiveRelativePath = this.pathByDate | ||
| ? pathJoin(dayjs().format('YYYY'), dayjs().format('MM'), fileName) | ||
| : fileName | ||
|
Comment on lines
+156
to
+158
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Call
Proposed fix- const effectiveRelativePath = this.pathByDate
- ? pathJoin(dayjs().format('YYYY'), dayjs().format('MM'), fileName)
- : fileName
+ const now = dayjs()
+ const effectiveRelativePath = this.pathByDate
+ ? pathJoin(now.format('YYYY'), now.format('MM'), fileName)
+ : fileName🤖 Prompt for AI Agents |
||
| const filePath = pathJoin(path, effectiveRelativePath) | ||
| const d = await this.createFile({ | ||
| owner, | ||
| repo, | ||
| path: pathJoin(path, fileName), | ||
| path: filePath, | ||
| message: `Upload ${fileName} by picGo - ${getNow()}`, | ||
| content: img.base64Image || Buffer.from(img.buffer).toString('base64'), | ||
| branch | ||
| }) | ||
| if (d) { | ||
| return { | ||
| imgUrl: this.parseUrl(fileName), | ||
| sha: d.data.content.sha | ||
| imgUrl: this.parseUrl(effectiveRelativePath), | ||
| sha: d.data.content.sha, | ||
| fileName: this.pathByDate ? effectiveRelativePath : undefined | ||
| } | ||
| } | ||
| /* istanbul ignore next */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getTreereturn type is missing thetypefield used here.Line 77 accesses
item.typeon results fromthis.getTree(sha)(Line 75), butgetTree(Line 47) declares its return type as{ path: string; sha: string }[]— there's notypeproperty. The GitHub API does return it at runtime, but the type declaration should be fixed to avoid silentundefinedchecks if TypeScript strict mode is ever enabled.Proposed fix
Additionally, the recursion is sequential (
awaitin aforloop). For repos with many subdirectories this could be slow, but it's acceptable for typical image hosting use cases.🤖 Prompt for AI Agents