<commit_before>import { SURMBLOG_AWS_BUCKET_NAME, SURMBLOG_AWS_BUCKET_REGION } from "env:";
import { decode } from "./jwt.js";
import unindent from "./unindent.js";
import { render } from "preact";
import { html } from "htm/preact";

const TOKEN_KEY = "token";
function getToken() {
  const params = new URLSearchParams(location.search);
  if (params.has(TOKEN_KEY)) {
    return params.get(TOKEN_KEY);
  }
}
let token = getToken();

async function shaHash(buffer, hash = "SHA-256") {
  const digest = await crypto.subtle.digest(hash, buffer);
  return [...new Uint8Array(digest)].map(o => o.toString(16)).join("");
}

function ext(name) {
  const match = /\.([^.]+)$/.exec(name);
  if (!match) {
    return "";
  }
  return match[1];
}

async function submit(evt) {
  evt.preventDefault();
  const { dateField, fileField, locationField } = evt.target;
  const file = fileField.files[0];
  const fileExt = ext(file.name);
  const buffer = await new Response(file).arrayBuffer();
  const hash = await shaHash(buffer);
  const req = {
    host: `s3.${SURMBLOG_AWS_BUCKET_REGION}.amazonaws.com`,
    method: "PUT",
    path: `/${SURMBLOG_AWS_BUCKET_NAME}/${hash}.${fileExt}`,
    headers: {
      "Content-Length": buffer.length,
      "x-amz-content-sha256": "UNSIGNED-PAYLOAD"
    }
  };
  const signedReq = await (await fetch("/.netlify/functions/sign_request", {
    method: "POST",
    body: JSON.stringify(req),
    headers: {
      Authorization: `Bearer ${token}`,
      "Content-Type": "application/json"
    }
  })).json();
  const url = new URL(signedReq.path, `https://${signedReq.host}`).toString();
  const upload = await fetch(url, {
    ...signedReq,
    body: buffer
  });
  if (!upload.ok) {
    alert("Upload to S3 failed");
    return;
  }
  const { access_token, token_type } = decode(token);
  const r = await fetch(
    `https://api.github.com/repos/surma/surma.github.io/contents/content/photography/${date.value}.md`,
    {
      method: "PUT",
      headers: {
        "Content-Type": "application/json",
        Authorization: `${token_type} ${access_token}`<commit_msg>Another small fix<commit_after>import { SURMBLOG_AWS_BUCKET_NAME, SURMBLOG_AWS_BUCKET_REGION } from "env:";
import { decode } from "./jwt.js";
import unindent from "./unindent.js";
import { render } from "preact";
import { html } from "htm/preact";

const TOKEN_KEY = "token";
function getToken() {
  const params = new URLSearchParams(location.search);
  if (params.has(TOKEN_KEY)) {
    return params.get(TOKEN_KEY);
  }
}
let token = getToken();

async function shaHash(buffer, hash = "SHA-256") {
  const digest = await crypto.subtle.digest(hash, buffer);
  return [...new Uint8Array(digest)].map(o => o.toString(16)).join("");
}

function ext(name) {
  const match = /\.([^.]+)$/.exec(name);
  if (!match) {
    return "";
  }
  return match[1];
}

async function submit(evt) {
  evt.preventDefault();
  const { dateField, fileField, locationField } = evt.target;
  const file = fileField.files[0];
  const fileExt = ext(file.name);
  const buffer = await new Response(file).arrayBuffer();
  const hash = await shaHash(buffer);
  const req = {
    host: `s3.${SURMBLOG_AWS_BUCKET_REGION}.amazonaws.com`,
    method: "PUT",
    path: `/${SURMBLOG_AWS_BUCKET_NAME}/${hash}.${fileExt}`,
    headers: {
      "Content-Length": buffer.length,
      "x-amz-content-sha256": "UNSIGNED-PAYLOAD"
    }
  };
  const signedReq = await (await fetch("/.netlify/functions/sign_request", {
    method: "POST",
    body: JSON.stringify(req),
    headers: {
      Authorization: `Bearer ${token}`,
      "Content-Type": "application/json"
    }
  })).json();
  const url = new URL(signedReq.path, `https://${signedReq.host}`).toString();
  const upload = await fetch(url, {
    ...signedReq,
    body: buffer
  });
  if (!upload.ok) {
    alert("Upload to S3 failed");
    return;
  }
  const { access_token, token_type } = decode(token);
  const r = await fetch(
    `https://api.github.com/repos/surma/surma.github.io/contents/content/photography/${dateField.value}.md`,
    {
      method: "PUT",
      headers: {
        "Content-Type": "application/json",
        Authorization: `${token_type} ${access_token}`
{'fraction_non_alphanumeric': 0.17155110793423875, 'fraction_numerical': 0.0061949011198475104, 'mean_word_length': 3.5879781420765027, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1947994', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1467, 'n_words': 301}