This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Storage } from "@google-cloud/storage"; | |
const storage = new Storage({ | |
keyFilename: "KEY_FILENAME.json", | |
}); | |
const bucket = storage.bucket(process.env.GCS_BUCKET as string); | |
export const createWriteStream = (filename: string, contentType?: string) => { | |
const ref = bucket.file(filename); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require("dotenv").config(); | |
import { createServer } from "http"; | |
import next from "next"; | |
import express from "express"; | |
import { method2 } from "./lib/upload"; | |
const port = parseInt(process.env.PORT || "3000", 10); | |
const dev = process.env.NODE_ENV !== "production"; | |
const nextApp = next({ dev }); | |
const handle = nextApp.getRequestHandler(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
import { PassThrough } from "stream"; | |
const uploadStream = (file: formidable.File) => { | |
const pass = new PassThrough(); | |
const stream = gcs.createWriteStream( | |
file.originalFilename ?? file.newFilename, | |
file.mimetype ?? undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const method2 = ( | |
req: NextApiRequest | IncomingMessage, | |
res: NextApiResponse | Response | |
) => { | |
// @ts-ignore | |
const form = formidable({ fileWriteStreamHandler: uploadStream }); | |
form.parse(req, () => { | |
res.status(200).json("File upload complete"); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import formidable from "./formidable-serverless"; | |
import { createReadStream } from "fs"; | |
import { IncomingMessage } from "http"; | |
import { NextApiRequest, NextApiResponse } from "next"; | |
import parseForm from "./parseForm"; | |
import * as gcs from "./gcs"; | |
import { Response } from "express"; | |
export const method1 = async ( | |
req: NextApiRequest | IncomingMessage, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { NextApiRequest, NextApiResponse } from "next"; | |
import { method1 } from "../../lib/upload"; | |
export default async function handler( | |
req: NextApiRequest, | |
res: NextApiResponse | |
) { | |
if (req.method !== "POST") { | |
res.status(400).send(`Invalid method: ${req.method}`); | |
return; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import formidable from "./formidable-serverless"; | |
import IncomingForm from "formidable/Formidable"; | |
import { IncomingMessage } from "http"; | |
const parseForm = async ( | |
form: IncomingForm, | |
req: IncomingMessage | |
): Promise<{ fields: formidable.Fields; files: formidable.Files }> => { | |
return await new Promise(async (resolve, reject) => { | |
form.parse(req, function (err, fields, files) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Storage } from "@google-cloud/storage"; | |
const storage = new Storage({ | |
keyFilename: "KEY_FILENAME.json", | |
}); | |
const bucket = storage.bucket(process.env.GCS_BUCKET as string); | |
export const createWriteStream = (filename: string, contentType?: string) => { | |
const ref = bucket.file(filename); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/routes/listen.jsx | |
import { useRef } from "react"; | |
import { io } from "socket.io-client"; | |
import useAudioStreamer from "../hooks/useAudioStreamer"; | |
import { Headphones } from "react-feather"; | |
const URL = "http://localhost:3000"; | |
function Listen() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/hooks/useAudioStreamer.js | |
import { AudioStreamer } from "jnaudiostream"; | |
import { useEffect, useRef } from "react"; | |
const useAudioStreamer = (socket) => { | |
const streamerRef = useRef(new AudioStreamer()); | |
useEffect(() => { | |
const streamer = streamerRef.current; |