mirror of
https://github.com/Stef-00012/Zipline-Android-App.git
synced 2025-05-11 10:25:59 +02:00
remove comments, handle tags
This commit is contained in:
parent
b9591efc19
commit
a36961119f
28 changed files with 956 additions and 486 deletions
|
@ -1,14 +1,9 @@
|
|||
import mimetypesJSON from "@/assets/mimetypes.json";
|
||||
import type { Mimetypes } from "@/types/mimetypes";
|
||||
import axios from "axios";
|
||||
import * as FileSystem from "expo-file-system";
|
||||
|
||||
const mimetypes = mimetypesJSON as Mimetypes;
|
||||
|
||||
export function generateRandomString(): string {
|
||||
return Math.random().toString(36).substring(2, 6);
|
||||
}
|
||||
|
||||
export function guessMimetype(
|
||||
mimetype: keyof Mimetypes,
|
||||
): Mimetypes[keyof Mimetypes] {
|
||||
|
@ -25,62 +20,14 @@ export function guessExtension(
|
|||
): keyof Mimetypes {
|
||||
if (!mimetype) return "application/octet-stream";
|
||||
|
||||
const mime = Object.entries(mimetypes).find(([key, value]) => value === mimetype) as [keyof Mimetypes, Mimetypes[keyof Mimetypes]] | undefined;
|
||||
const mime = Object.entries(mimetypes).find(
|
||||
([key, value]) => value === mimetype,
|
||||
) as [keyof Mimetypes, Mimetypes[keyof Mimetypes]] | undefined;
|
||||
if (!mime) return "application/octet-stream";
|
||||
|
||||
return mime[0];
|
||||
}
|
||||
|
||||
// export function convertToBlob(data: string): Blob {
|
||||
// console.debug("ctb", 1)
|
||||
|
||||
// const base64Data = data.split(",")[1];
|
||||
// // console.debug("ctb", 2, base64Data)
|
||||
// const mimetype = data.split(":")[1].split(";").shift();
|
||||
// console.debug("ctb", 3, mimetype)
|
||||
// const string = atob(base64Data);
|
||||
// const length = string.length;
|
||||
// console.debug("ctb", 4, length)
|
||||
// const bytes = new Uint8Array(length);
|
||||
|
||||
// for (let i = 0; i < length; i++) {
|
||||
// console.debug("ctb", 4.5, i, length)
|
||||
// bytes[i] = string.charCodeAt(i);
|
||||
// }
|
||||
|
||||
// const blob = new Blob([bytes], { type: mimetype || "image/png" });
|
||||
|
||||
// console.debug("ctb", 5, blob)
|
||||
|
||||
// return blob;
|
||||
// }
|
||||
|
||||
// export function convertToBlob(utf8Data: string, mimetype?: string): Blob {
|
||||
// console.debug("ctb", 1)
|
||||
// const byteCharacters = new TextEncoder().encode(utf8Data);
|
||||
// console.debug(byteCharacters, byteCharacters.length)
|
||||
// console.debug("ctb", 2)
|
||||
|
||||
// const blob = new Blob([byteCharacters], { type: mimetype });
|
||||
// console.debug("ctb", 3)
|
||||
|
||||
// console.debug(blob)
|
||||
|
||||
// return blob;
|
||||
// }
|
||||
|
||||
export async function convertToBlob(fileURI: string): Promise<Blob | null> {
|
||||
try {
|
||||
const res = await axios.get(fileURI, {
|
||||
responseType: "blob"
|
||||
})
|
||||
|
||||
return res.data;
|
||||
} catch(e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getFileDataURI(filePath: string): Promise<string | null> {
|
||||
const base64Data = await FileSystem.readAsStringAsync(filePath, {
|
||||
encoding: FileSystem.EncodingType.Base64,
|
||||
|
@ -88,7 +35,7 @@ export async function getFileDataURI(filePath: string): Promise<string | null> {
|
|||
|
||||
const extension = filePath.split(".").pop();
|
||||
|
||||
const mimetype = guessExtension(extension as Mimetypes[keyof Mimetypes])
|
||||
const mimetype = guessExtension(extension as Mimetypes[keyof Mimetypes]);
|
||||
|
||||
const dataURI = `data:${mimetype};base64,${base64Data}`;
|
||||
|
||||
|
@ -134,4 +81,20 @@ export function timeDifference(currentDate: Date, targetDate: Date) {
|
|||
|
||||
const difference = Math.round(absElapsed / msPerYear);
|
||||
return `${isFuture ? "in " : ""}${difference} year${difference !== 1 ? "s" : ""}${isFuture ? "" : " ago"}`;
|
||||
}
|
||||
}
|
||||
|
||||
export function colorHash(str: string) {
|
||||
let hash = 0;
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
hash = str.charCodeAt(i) + ((hash << 5) - hash);
|
||||
}
|
||||
|
||||
let color = "#";
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const value = (hash >> (i * 8)) & 0xff;
|
||||
color += `00${value.toString(16)}`.substr(-2);
|
||||
}
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue