fix onedrive upload issue!

This commit is contained in:
fyears 2022-04-01 23:39:41 +08:00
parent 714ea43613
commit 953507ec43
3 changed files with 24 additions and 8 deletions

View File

@ -5,6 +5,8 @@
import type { LangType, LangTypeAndAuto } from "./i18n"; import type { LangType, LangTypeAndAuto } from "./i18n";
export const DEFAULT_CONTENT_TYPE = "application/octet-stream";
export type SUPPORTED_SERVICES_TYPE = "s3" | "webdav" | "dropbox" | "onedrive"; export type SUPPORTED_SERVICES_TYPE = "s3" | "webdav" | "dropbox" | "onedrive";
export type SUPPORTED_SERVICES_TYPE_WITH_REMOTE_BASE_DIR = export type SUPPORTED_SERVICES_TYPE_WITH_REMOTE_BASE_DIR =

View File

@ -11,6 +11,7 @@ import { request, requestUrl, requireApiVersion, Vault } from "obsidian";
import { import {
API_VER_REQURL, API_VER_REQURL,
COMMAND_CALLBACK_ONEDRIVE, COMMAND_CALLBACK_ONEDRIVE,
DEFAULT_CONTENT_TYPE,
OAUTH2_FORCE_EXPIRE_MILLISECONDS, OAUTH2_FORCE_EXPIRE_MILLISECONDS,
OnedriveConfig, OnedriveConfig,
RemoteItem, RemoteItem,
@ -505,12 +506,17 @@ export class WrappedOnedriveClient {
putArrayBuffer = async (pathFragOrig: string, payload: ArrayBuffer) => { putArrayBuffer = async (pathFragOrig: string, payload: ArrayBuffer) => {
const theUrl = this.buildUrl(pathFragOrig); const theUrl = this.buildUrl(pathFragOrig);
log.debug(`putArrayBuffer, theUrl=${theUrl}`); log.debug(`putArrayBuffer, theUrl=${theUrl}`);
if (requireApiVersion(API_VER_REQURL)) { // TODO:
// 20220401: On Android, requestUrl has issue that text becomes base64.
// Use fetch everywhere instead!
if (false /*requireApiVersion(API_VER_REQURL)*/) {
await requestUrl({ await requestUrl({
url: theUrl, url: theUrl,
method: "PUT", method: "PUT",
body: payload, body: payload,
contentType: DEFAULT_CONTENT_TYPE,
headers: { headers: {
"Content-Type": DEFAULT_CONTENT_TYPE,
Authorization: `Bearer ${await this.authGetter.getAccessToken()}`, Authorization: `Bearer ${await this.authGetter.getAccessToken()}`,
}, },
}); });
@ -519,6 +525,7 @@ export class WrappedOnedriveClient {
method: "PUT", method: "PUT",
body: payload, body: payload,
headers: { headers: {
"Content-Type": DEFAULT_CONTENT_TYPE,
Authorization: `Bearer ${await this.authGetter.getAccessToken()}`, Authorization: `Bearer ${await this.authGetter.getAccessToken()}`,
}, },
}); });
@ -547,15 +554,18 @@ export class WrappedOnedriveClient {
}, len=${rangeEnd - rangeStart}, size=${size}` }, len=${rangeEnd - rangeStart}, size=${size}`
); );
// NO AUTH HEADER here! // NO AUTH HEADER here!
if (requireApiVersion(API_VER_REQURL)) { // TODO:
// 20220401: On Android, requestUrl has issue that text becomes base64.
// Use fetch everywhere instead!
if (false /*requireApiVersion(API_VER_REQURL)*/) {
const res = await requestUrl({ const res = await requestUrl({
url: theUrl, url: theUrl,
method: "PUT", method: "PUT",
body: bufferToArrayBuffer(payload.subarray(rangeStart, rangeEnd)), body: bufferToArrayBuffer(payload.subarray(rangeStart, rangeEnd)),
contentType: DEFAULT_CONTENT_TYPE,
headers: { headers: {
// no "Content-Length" allowed here // no "Content-Length" allowed here
"Content-Range": `bytes ${rangeStart}-${rangeEnd - 1}/${size}`, "Content-Range": `bytes ${rangeStart}-${rangeEnd - 1}/${size}`,
"Content-Type": "application/octet-stream",
}, },
}); });
return res.json as DriveItem | UploadSession; return res.json as DriveItem | UploadSession;
@ -566,7 +576,7 @@ export class WrappedOnedriveClient {
headers: { headers: {
"Content-Length": `${rangeEnd - rangeStart}`, "Content-Length": `${rangeEnd - rangeStart}`,
"Content-Range": `bytes ${rangeStart}-${rangeEnd - 1}/${size}`, "Content-Range": `bytes ${rangeStart}-${rangeEnd - 1}/${size}`,
"Content-Type": "application/octet-stream", "Content-Type": DEFAULT_CONTENT_TYPE,
}, },
}); });
return (await res.json()) as DriveItem | UploadSession; return (await res.json()) as DriveItem | UploadSession;
@ -737,8 +747,9 @@ export const uploadToRemote = async (
// hard code range size // hard code range size
const MIN_UNIT = 327680; // bytes in msft doc, about 0.32768 MB const MIN_UNIT = 327680; // bytes in msft doc, about 0.32768 MB
const RANGE_SIZE = MIN_UNIT * 20; // about 6.5536 MB const RANGE_SIZE = MIN_UNIT * 20; // about 6.5536 MB
const DIRECT_UPLOAD_MAX_SIZE = 1000 * 1000 * 4; // 4 Megabyte
if (remoteContent.byteLength <= RANGE_SIZE) { if (remoteContent.byteLength < DIRECT_UPLOAD_MAX_SIZE) {
// directly using put! // directly using put!
await client.putArrayBuffer( await client.putArrayBuffer(
`${uploadFile}:/content?${new URLSearchParams({ `${uploadFile}:/content?${new URLSearchParams({

View File

@ -30,7 +30,12 @@ import {
requireApiVersion, requireApiVersion,
} from "obsidian"; } from "obsidian";
import { Readable } from "stream"; import { Readable } from "stream";
import { API_VER_REQURL, RemoteItem, S3Config } from "./baseTypes"; import {
API_VER_REQURL,
DEFAULT_CONTENT_TYPE,
RemoteItem,
S3Config,
} from "./baseTypes";
import { decryptArrayBuffer, encryptArrayBuffer } from "./encrypt"; import { decryptArrayBuffer, encryptArrayBuffer } from "./encrypt";
import { import {
arrayBufferToBuffer, arrayBufferToBuffer,
@ -247,8 +252,6 @@ export const uploadToRemote = async (
} }
const isFolder = fileOrFolderPath.endsWith("/"); const isFolder = fileOrFolderPath.endsWith("/");
const DEFAULT_CONTENT_TYPE = "application/octet-stream";
if (isFolder && isRecursively) { if (isFolder && isRecursively) {
throw Error("upload function doesn't implement recursive function yet!"); throw Error("upload function doesn't implement recursive function yet!");
} else if (isFolder && !isRecursively) { } else if (isFolder && !isRecursively) {