-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
32 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,122 +1,22 @@ | ||
import { enhanceFetch } from './fetch'; | ||
import { obj2form } from './others'; | ||
import store from './store'; | ||
import { uploadPoliciesAssets } from 'user-attachments'; | ||
import type { PoliciesAsset } from 'user-attachments'; | ||
|
||
const authenticityTokenPageUrl = `https://github.com/gkd-kit/inspect/issues/new`; | ||
const repository_id = `661952005`; | ||
const commonHeaders = { | ||
origin: `https://github.com`, | ||
referer: authenticityTokenPageUrl, | ||
}; | ||
|
||
const getCsrfToken = async () => { | ||
const csrfSelector = `[data-upload-policy-url="/upload/policies/assets"] input.js-data-upload-policy-url-csrf`; | ||
const resp = await enhanceFetch(authenticityTokenPageUrl); | ||
const responseDoc = new DOMParser().parseFromString( | ||
await resp.text(), | ||
'text/html', | ||
); | ||
const csrfToken = responseDoc | ||
.querySelector(csrfSelector) | ||
?.getAttribute(`value`); | ||
return csrfToken; | ||
}; | ||
|
||
type S3Form = { | ||
key: string; | ||
acl: string; | ||
policy: string; | ||
'X-Amz-Algorithm': string; | ||
'X-Amz-Credential': string; | ||
'X-Amz-Date': string; | ||
'X-Amz-Signature': string; | ||
'Content-Type': string; | ||
'Cache-Control': string; | ||
'x-amz-meta-Surrogate-Control': string; | ||
}; | ||
export type GithubPoliciesAsset = { | ||
id: number; | ||
name: string; | ||
size: number; | ||
content_type: string; | ||
original_name: string; | ||
href: string; | ||
}; | ||
|
||
type UploadPoliciesAssetsRsonpse = { | ||
upload_url: string; | ||
header: object; | ||
asset: GithubPoliciesAsset; | ||
form: S3Form; | ||
same_origin: boolean; | ||
asset_upload_url: string; | ||
upload_authenticity_token: string; | ||
asset_upload_authenticity_token: string; | ||
}; | ||
export type GithubPoliciesAsset = PoliciesAsset; | ||
|
||
export const uploadPoliciesAssets = async ( | ||
export const uploadAsset = async ( | ||
bf: ArrayBuffer, | ||
name: string, | ||
content_type: string, | ||
): Promise<GithubPoliciesAsset> => { | ||
const authenticity_token = await getCsrfToken(); | ||
if (!authenticity_token) { | ||
store.githubErrorDlgVisible = true; | ||
throw new Error(`failed get csrfToken`); | ||
} | ||
|
||
const policiesResp: UploadPoliciesAssetsRsonpse = await enhanceFetch( | ||
`https://github.com/upload/policies/assets`, | ||
{ | ||
method: `POST`, | ||
body: obj2form({ | ||
authenticity_token, | ||
content_type, | ||
name, | ||
size: bf.byteLength, | ||
repository_id, | ||
}), | ||
headers: commonHeaders, | ||
}, | ||
).then((r) => { | ||
if (!r.ok) { | ||
throw new Error(`failed upload policies assets`); | ||
): Promise<PoliciesAsset> => { | ||
return await uploadPoliciesAssets({ | ||
file: new File([bf], name), | ||
url: 'https://github.com/gkd-kit/inspect/issues/1', | ||
fetch: enhanceFetch, | ||
}).catch((e) => { | ||
if (e instanceof Error && e.message === 'not found authenticity_token') { | ||
store.githubErrorDlgVisible = true; | ||
} | ||
return r.json(); | ||
}); | ||
|
||
// violentmonkey success | ||
// tampermonkey failed https://github.com/Tampermonkey/tampermonkey/issues/1783 | ||
// use fetch is also work, but console.error cors and can not get response | ||
const s3Resp = await enhanceFetch(policiesResp.upload_url, { | ||
method: `POST`, | ||
body: obj2form(policiesResp.form, { | ||
file: new File([bf], name, { type: content_type }), | ||
}), | ||
headers: commonHeaders, | ||
throw e; | ||
}); | ||
if (!s3Resp.ok) { | ||
throw new Error(`upload s3 failed`); | ||
} | ||
|
||
const assetsResp = await enhanceFetch( | ||
new URL(policiesResp.asset_upload_url, `https://github.com/`).href, | ||
{ | ||
method: `PUT`, | ||
body: obj2form({ | ||
authenticity_token: policiesResp.asset_upload_authenticity_token, | ||
}), | ||
headers: { | ||
...commonHeaders, | ||
// api must add `Accept` request headers | ||
Accept: `application/json`, | ||
}, | ||
}, | ||
); | ||
|
||
if (assetsResp.status != 200) { | ||
throw new Error(`failed check authenticity upload`); | ||
} | ||
|
||
return policiesResp.asset; | ||
}; |