mirror of https://github.com/MaxLeiter/Drift
fix middleware, migrate gist importing
parent
bc2a4acd29
commit
8e7828d562
@ -0,0 +1,11 @@
|
||||
import { getAllPosts, getAllUsers } from "@lib/server/prisma"
|
||||
import styles from "./admin.module.css"
|
||||
import PostTable from "./post-table"
|
||||
import UserTable from "./user-table"
|
||||
|
||||
const Admin = async () => {
|
||||
|
||||
)
|
||||
}
|
||||
|
||||
export default Admin
|
||||
@ -1,5 +1,3 @@
|
||||
import fetch from "node-fetch"
|
||||
import { Response } from "node-fetch"
|
||||
import { Gist, GistFile } from "./types"
|
||||
|
||||
async function fetchHelper(response: Response): Promise<Response> {
|
||||
@ -0,0 +1,68 @@
|
||||
import { Gist } from "./types"
|
||||
import * as crypto from "crypto"
|
||||
import type { Post } from "@lib/server/prisma"
|
||||
import { getHtmlFromFile } from "@lib/server/get-html-from-drift-file"
|
||||
import { prisma } from "@lib/server/prisma"
|
||||
|
||||
export type AdditionalPostInformation = Pick<
|
||||
Post,
|
||||
"visibility" | "password" | "expiresAt"
|
||||
> & {
|
||||
userId: string
|
||||
}
|
||||
|
||||
export async function createPostFromGist(
|
||||
{ userId, visibility, password, expiresAt }: AdditionalPostInformation,
|
||||
gist: Gist
|
||||
): Promise<Post> {
|
||||
const files = Object.values(gist.files)
|
||||
const [title, description] = gist.description.split("\n", 1)
|
||||
|
||||
if (files.length === 0) {
|
||||
throw new Error("The gist did not have any files")
|
||||
}
|
||||
|
||||
const newPost = await prisma.post.create({
|
||||
data: {
|
||||
title,
|
||||
description,
|
||||
visibility,
|
||||
password,
|
||||
expiresAt,
|
||||
createdAt: new Date(gist.created_at),
|
||||
author: {
|
||||
connect: {
|
||||
id: userId
|
||||
}
|
||||
},
|
||||
files: {
|
||||
createMany: {
|
||||
data: await Promise.all(
|
||||
files.map(async (file) => {
|
||||
const content = await file.content()
|
||||
const html = await getHtmlFromFile({
|
||||
content,
|
||||
title: file.filename
|
||||
})
|
||||
|
||||
return {
|
||||
title: file.filename,
|
||||
content: content,
|
||||
sha: crypto
|
||||
.createHash("sha256")
|
||||
.update(content)
|
||||
.digest("hex")
|
||||
.toString(),
|
||||
// TODO: shouldn't need this cast
|
||||
html: html as string,
|
||||
userId: userId
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return newPost
|
||||
}
|
||||
@ -1,65 +0,0 @@
|
||||
import getHtmlFromFile from "@lib/get-html-from-drift-file"
|
||||
import { Post } from "@lib/models/Post"
|
||||
import { File } from "@lib/models/File"
|
||||
import { Gist } from "./types"
|
||||
import * as crypto from "crypto"
|
||||
|
||||
export type AdditionalPostInformation = Pick<
|
||||
Post,
|
||||
"visibility" | "password" | "expiresAt"
|
||||
> & {
|
||||
userId: string
|
||||
}
|
||||
|
||||
export async function createPostFromGist(
|
||||
{ userId, visibility, password, expiresAt }: AdditionalPostInformation,
|
||||
gist: Gist
|
||||
): Promise<Post> {
|
||||
const files = Object.values(gist.files)
|
||||
const [title, description] = gist.description.split("\n", 1)
|
||||
|
||||
if (files.length === 0) {
|
||||
throw new Error("The gist did not have any files")
|
||||
}
|
||||
|
||||
const newPost = new Post({
|
||||
title,
|
||||
description,
|
||||
visibility,
|
||||
password,
|
||||
expiresAt,
|
||||
createdAt: new Date(gist.created_at)
|
||||
})
|
||||
|
||||
await newPost.save()
|
||||
await newPost.$add("users", userId)
|
||||
const newFiles = await Promise.all(
|
||||
files.map(async (file) => {
|
||||
const content = await file.content()
|
||||
const html = getHtmlFromFile({ content, title: file.filename })
|
||||
const newFile = new File({
|
||||
title: file.filename,
|
||||
content,
|
||||
sha: crypto
|
||||
.createHash("sha256")
|
||||
.update(content)
|
||||
.digest("hex")
|
||||
.toString(),
|
||||
html: html || "",
|
||||
userId: userId,
|
||||
postId: newPost.id
|
||||
})
|
||||
await newFile.save()
|
||||
return newFile
|
||||
})
|
||||
)
|
||||
|
||||
await Promise.all(
|
||||
newFiles.map(async (file) => {
|
||||
await newPost.$add("files", file.id)
|
||||
await newPost.save()
|
||||
})
|
||||
)
|
||||
|
||||
return newPost
|
||||
}
|
||||
Loading…
Reference in New Issue