🎉 Init
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
const UNITS = ['B', 'KiB', 'MiB', 'GiB', 'TiB']
|
||||
|
||||
export function formatBytes(bytes: number): string {
|
||||
let value = bytes
|
||||
let unitIndex = 0
|
||||
while (value >= 1024 && unitIndex < UNITS.length - 1) {
|
||||
value /= 1024
|
||||
unitIndex += 1
|
||||
}
|
||||
return `${value.toFixed(unitIndex === 0 ? 0 : 1)} ${UNITS[unitIndex]}`
|
||||
}
|
||||
|
||||
export function formatTime(iso: string): string {
|
||||
return new Date(iso).toLocaleTimeString('de-DE', { hour: '2-digit', minute: '2-digit', second: '2-digit' })
|
||||
}
|
||||
|
||||
export function formatDate(iso: string): string {
|
||||
return new Date(iso).toLocaleDateString('de-DE', { day: '2-digit', month: '2-digit', year: 'numeric' })
|
||||
}
|
||||
|
||||
export function isToday(iso: string): boolean {
|
||||
const date = new Date(iso)
|
||||
const now = new Date()
|
||||
return date.getFullYear() === now.getFullYear() && date.getMonth() === now.getMonth() && date.getDate() === now.getDate()
|
||||
}
|
||||
|
||||
export function formatDateTime(iso: string): string {
|
||||
return new Date(iso).toLocaleString('de-DE', { dateStyle: 'medium', timeStyle: 'medium' })
|
||||
}
|
||||
|
||||
export function partCount(etag: string): number {
|
||||
const dashIndex = etag.indexOf('-')
|
||||
if (dashIndex === -1) {
|
||||
return 1
|
||||
}
|
||||
const parsed = Number(etag.slice(dashIndex + 1))
|
||||
return Number.isFinite(parsed) && parsed > 0 ? parsed : 1
|
||||
}
|
||||
|
||||
export function shortEtag(etag: string): string {
|
||||
const dashIndex = etag.indexOf('-')
|
||||
const hash = dashIndex === -1 ? etag : etag.slice(0, dashIndex)
|
||||
const suffix = dashIndex === -1 ? '' : etag.slice(dashIndex)
|
||||
return `${hash.slice(0, 8)}…${hash.slice(-4)}${suffix}`
|
||||
}
|
||||
|
||||
export function baseName(key: string): string {
|
||||
const slashIndex = key.lastIndexOf('/')
|
||||
return slashIndex === -1 ? key : key.slice(slashIndex + 1)
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import type { Component } from 'vue'
|
||||
|
||||
export class Popup<P> {
|
||||
component?: Component
|
||||
payload?: P
|
||||
config?: PopupConfig
|
||||
|
||||
public static component<P>(component: Component): Popup<P> {
|
||||
const popup = new Popup<P>()
|
||||
popup.component = component
|
||||
return popup
|
||||
}
|
||||
|
||||
public setPayload(payload: P) {
|
||||
this.payload = payload
|
||||
return this
|
||||
}
|
||||
|
||||
public setConfig(config: PopupConfig) {
|
||||
this.config = config
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
export interface PopupConfig {
|
||||
heading: string
|
||||
size: PopupSize
|
||||
closeOnOverlayClick?: boolean
|
||||
callback?: (payload?: unknown) => void
|
||||
}
|
||||
|
||||
export enum PopupSize {
|
||||
SMALL = 'small',
|
||||
MEDIUM = 'medium',
|
||||
LARGE = 'large',
|
||||
FULL = 'full',
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
export interface S3Bucket {
|
||||
name: string
|
||||
creationDate: string
|
||||
}
|
||||
|
||||
export interface S3Object {
|
||||
key: string
|
||||
size: number
|
||||
lastModified: string
|
||||
etag: string
|
||||
}
|
||||
|
||||
export function parseBucketsXml(xml: string): S3Bucket[] {
|
||||
const doc = new DOMParser().parseFromString(xml, 'application/xml')
|
||||
return Array.from(doc.getElementsByTagName('Bucket')).map((el) => ({
|
||||
name: text(el, 'Name'),
|
||||
creationDate: text(el, 'CreationDate'),
|
||||
}))
|
||||
}
|
||||
|
||||
export function parseObjectsXml(xml: string): S3Object[] {
|
||||
const doc = new DOMParser().parseFromString(xml, 'application/xml')
|
||||
return Array.from(doc.getElementsByTagName('Contents')).map((el) => ({
|
||||
key: text(el, 'Key'),
|
||||
size: Number(text(el, 'Size')),
|
||||
lastModified: text(el, 'LastModified'),
|
||||
etag: text(el, 'ETag').replace(/"/g, ''),
|
||||
}))
|
||||
}
|
||||
|
||||
function text(parent: Element, tag: string): string {
|
||||
return parent.getElementsByTagName(tag)[0]?.textContent ?? ''
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
const REGION = 'us-east-1'
|
||||
const SERVICE = 's3'
|
||||
|
||||
export interface SignedHeaders {
|
||||
Authorization: string
|
||||
'X-Amz-Date': string
|
||||
'X-Amz-Content-Sha256': string
|
||||
}
|
||||
|
||||
export async function signS3Get(path: string, accessKey: string, secretKey: string): Promise<SignedHeaders> {
|
||||
return signS3('GET', path, await sha256Hex(''), accessKey, secretKey)
|
||||
}
|
||||
|
||||
export async function signS3Put(path: string, accessKey: string, secretKey: string): Promise<SignedHeaders> {
|
||||
return signS3('PUT', path, 'UNSIGNED-PAYLOAD', accessKey, secretKey)
|
||||
}
|
||||
|
||||
export async function signS3Delete(path: string, accessKey: string, secretKey: string): Promise<SignedHeaders> {
|
||||
return signS3('DELETE', path, await sha256Hex(''), accessKey, secretKey)
|
||||
}
|
||||
|
||||
async function signS3(method: string, path: string, payloadHash: string, accessKey: string, secretKey: string): Promise<SignedHeaders> {
|
||||
const amzDate = toAmzDate(new Date())
|
||||
const dateStamp = amzDate.slice(0, 8)
|
||||
|
||||
const signedHeaderValues: Record<string, string> = {
|
||||
'x-amz-content-sha256': payloadHash,
|
||||
'x-amz-date': amzDate,
|
||||
}
|
||||
const signedHeaders = Object.keys(signedHeaderValues).join(';')
|
||||
const canonicalHeaders = Object.entries(signedHeaderValues)
|
||||
.map(([name, value]) => `${name}:${value}\n`)
|
||||
.join('')
|
||||
|
||||
const canonicalRequest = [method, encodeCanonicalPath(path), '', canonicalHeaders, signedHeaders, payloadHash].join('\n')
|
||||
const credentialScope = `${dateStamp}/${REGION}/${SERVICE}/aws4_request`
|
||||
const stringToSign = [
|
||||
'AWS4-HMAC-SHA256',
|
||||
amzDate,
|
||||
credentialScope,
|
||||
await sha256Hex(canonicalRequest),
|
||||
].join('\n')
|
||||
|
||||
const key = await deriveSigningKey(secretKey, dateStamp)
|
||||
const signature = bufferToHex(await hmac(key, stringToSign))
|
||||
|
||||
return {
|
||||
Authorization: `AWS4-HMAC-SHA256 Credential=${accessKey}/${credentialScope}, SignedHeaders=${signedHeaders}, Signature=${signature}`,
|
||||
'X-Amz-Date': amzDate,
|
||||
'X-Amz-Content-Sha256': payloadHash,
|
||||
}
|
||||
}
|
||||
|
||||
function toAmzDate(date: Date): string {
|
||||
return date.toISOString().replace(/[:-]|\.\d{3}/g, '')
|
||||
}
|
||||
|
||||
function isUnreservedByte(byte: number): boolean {
|
||||
return (byte >= 0x41 && byte <= 0x5a) // A-Z
|
||||
|| (byte >= 0x61 && byte <= 0x7a) // a-z
|
||||
|| (byte >= 0x30 && byte <= 0x39) // 0-9
|
||||
|| byte === 0x2d || byte === 0x5f || byte === 0x2e || byte === 0x7e // - _ . ~
|
||||
}
|
||||
|
||||
export function encodeCanonicalPath(path: string): string {
|
||||
return path
|
||||
.split('/')
|
||||
.map((segment) => Array.from(new TextEncoder().encode(segment))
|
||||
.map((byte) => (isUnreservedByte(byte) ? String.fromCharCode(byte) : `%${byte.toString(16).toUpperCase().padStart(2, '0')}`))
|
||||
.join(''))
|
||||
.join('/')
|
||||
}
|
||||
|
||||
async function deriveSigningKey(secretKey: string, dateStamp: string): Promise<ArrayBuffer> {
|
||||
const kDate = await hmac(new TextEncoder().encode(`AWS4${secretKey}`), dateStamp)
|
||||
const kRegion = await hmac(kDate, REGION)
|
||||
const kService = await hmac(kRegion, SERVICE)
|
||||
return hmac(kService, 'aws4_request')
|
||||
}
|
||||
|
||||
async function hmac(key: BufferSource, data: string): Promise<ArrayBuffer> {
|
||||
const cryptoKey = await crypto.subtle.importKey('raw', key, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign'])
|
||||
return crypto.subtle.sign('HMAC', cryptoKey, new TextEncoder().encode(data))
|
||||
}
|
||||
|
||||
async function sha256Hex(data: string): Promise<string> {
|
||||
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(data))
|
||||
return bufferToHex(digest)
|
||||
}
|
||||
|
||||
function bufferToHex(buffer: ArrayBuffer): string {
|
||||
return Array.from(new Uint8Array(buffer))
|
||||
.map((byte) => byte.toString(16).padStart(2, '0'))
|
||||
.join('')
|
||||
}
|
||||
Reference in New Issue
Block a user