2022-08-30 16:13:11 +05:00
|
|
|
/* This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2022 Streetwriters (Private) Limited
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-08-26 16:19:39 +05:00
|
|
|
import RNFetchBlob from "rn-fetch-blob";
|
|
|
|
|
import { useAttachmentStore } from "../../stores/use-attachment-store";
|
|
|
|
|
import { db } from "../database";
|
|
|
|
|
import { cacheDir } from "./utils";
|
2022-02-28 13:48:59 +05:00
|
|
|
|
|
|
|
|
export async function uploadFile(filename, data, cancelToken) {
|
|
|
|
|
if (!data) return false;
|
|
|
|
|
let { url, headers } = data;
|
|
|
|
|
|
2022-08-26 16:19:39 +05:00
|
|
|
console.log("uploading file: ", filename, headers);
|
2022-02-28 13:48:59 +05:00
|
|
|
try {
|
|
|
|
|
let res = await fetch(url, {
|
2022-08-26 16:19:39 +05:00
|
|
|
method: "PUT",
|
2022-02-28 13:48:59 +05:00
|
|
|
headers
|
|
|
|
|
});
|
|
|
|
|
if (!res.ok) throw new Error(`${res.status}: Unable to resolve upload url`);
|
|
|
|
|
const uploadUrl = await res.text();
|
2022-08-26 16:19:39 +05:00
|
|
|
if (!uploadUrl) throw new Error("Unable to resolve upload url");
|
2022-02-28 13:48:59 +05:00
|
|
|
|
|
|
|
|
let request = RNFetchBlob.config({
|
|
|
|
|
IOSBackgroundTask: true
|
|
|
|
|
})
|
|
|
|
|
.fetch(
|
2022-08-26 16:19:39 +05:00
|
|
|
"PUT",
|
2022-02-28 13:48:59 +05:00
|
|
|
uploadUrl,
|
|
|
|
|
{
|
2022-08-26 16:19:39 +05:00
|
|
|
"content-type": ""
|
2022-02-28 13:48:59 +05:00
|
|
|
},
|
|
|
|
|
RNFetchBlob.wrap(`${cacheDir}/${filename}`)
|
|
|
|
|
)
|
|
|
|
|
.uploadProgress((sent, total) => {
|
2022-08-26 16:19:39 +05:00
|
|
|
useAttachmentStore
|
|
|
|
|
.getState()
|
|
|
|
|
.setProgress(sent, total, filename, 0, "upload");
|
|
|
|
|
console.log("uploading: ", sent, total);
|
2022-02-28 13:48:59 +05:00
|
|
|
});
|
|
|
|
|
cancelToken.cancel = request.cancel;
|
|
|
|
|
let response = await request;
|
|
|
|
|
|
|
|
|
|
let status = response.info().status;
|
|
|
|
|
let text = await response.text();
|
|
|
|
|
let result = status >= 200 && status < 300 && text.length === 0;
|
|
|
|
|
useAttachmentStore.getState().remove(filename);
|
|
|
|
|
if (result) {
|
|
|
|
|
let attachment = db.attachments.attachment(filename);
|
|
|
|
|
if (!attachment) return result;
|
2022-08-26 16:19:39 +05:00
|
|
|
if (!attachment.metadata.type.startsWith("image/")) {
|
2022-02-28 13:48:59 +05:00
|
|
|
RNFetchBlob.fs.unlink(`${cacheDir}/${filename}`).catch(console.log);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
useAttachmentStore.getState().remove(filename);
|
2022-08-26 16:19:39 +05:00
|
|
|
console.log("upload file: ", e, url, headers);
|
2022-02-28 13:48:59 +05:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|