-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from COS301-SE-2024/feat/writeathons
Feat/writeathons
- Loading branch information
Showing
37 changed files
with
3,009 additions
and
568 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,35 @@ | ||
/* v8 ignore start */ | ||
import { generateHTML } from '@tiptap/html'; | ||
import type { JSONContent} from '@tiptap/core' | ||
|
||
import { auth } from '../../../../auth'; | ||
import { NextResponse } from 'next/server'; | ||
import { exportChapterSchema } from '../../../../db/chapter-schema'; | ||
import { getChapter } from '../../../../services/chapters'; | ||
import Paragraph from '@tiptap/extension-paragraph' | ||
import Text from '@tiptap/extension-text' | ||
import Bold from '@tiptap/extension-bold' | ||
import StarterKit from '@tiptap/starter-kit' | ||
|
||
import { any } from 'zod'; | ||
import { getPublishedChapter } from '../../../../services/chapters'; | ||
|
||
const puppeteer = require("puppeteer") | ||
|
||
export async function POST(req: Request){ | ||
export async function POST(req: Request) { | ||
try { | ||
const session = await auth(); | ||
|
||
if (!session?.user){ | ||
return new NextResponse(JSON.stringify({ | ||
status: 'fail', message: "You are not logged in", | ||
}), { status : 401}) | ||
if (!session?.user) { | ||
return new NextResponse( | ||
JSON.stringify({ status: 'fail', message: 'You are not logged in' }), | ||
{ status: 401 } | ||
); | ||
} | ||
|
||
const input = exportChapterSchema.parse(await req.json()); | ||
const chapter = await getPublishedChapter(input.id); | ||
|
||
const chapter = await getChapter(input.id); | ||
|
||
const content :JSONContent = { | ||
"type": 'doc', | ||
"content": <any>chapter.blocks, | ||
if (!chapter) { | ||
return new NextResponse( | ||
JSON.stringify({ status: 'fail', message: 'Chapter not found' }), | ||
{ status: 404 } | ||
); | ||
} | ||
|
||
// console.log(content) | ||
|
||
const html = generateHTML( content , [ | ||
StarterKit, | ||
]) | ||
|
||
console.log(html) | ||
return new NextResponse(JSON.stringify(chapter), { status: 200 }); | ||
} catch (error) { | ||
console.error('Error fetching chapter:', error); | ||
|
||
|
||
const browser = await puppeteer.launch(); | ||
const page = await browser.newPage(); | ||
await page.setContent(html, {waitUntil: 'networkidle0'}) | ||
const pdfBuffer = await page.pdf({format: 'A4'}) | ||
await browser.close(); | ||
const response = new NextResponse(pdfBuffer); | ||
|
||
response.headers.set("Content-Type", "application/pdf") | ||
console.log("render complete") | ||
|
||
return response; | ||
|
||
}catch (e) { | ||
console.log(e) | ||
return new NextResponse( | ||
JSON.stringify({ status: 'error', message: 'Failed to fetch chapter' }), | ||
{ status: 500 } | ||
); | ||
} | ||
|
||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,35 @@ | ||
/* v8 ignore start */ | ||
import { generateHTML } from '@tiptap/html'; | ||
import { auth } from '../../../../auth'; | ||
import { NextResponse } from 'next/server'; | ||
import { exportChapterSchema } from '../../../../db/chapter-schema'; | ||
import { getChapter } from '../../../../services/chapters'; | ||
import Paragraph from '@tiptap/extension-paragraph' | ||
import Text from '@tiptap/extension-text' | ||
import Bold from '@tiptap/extension-bold' | ||
import StarterKit from '@tiptap/starter-kit' | ||
|
||
import { any } from 'zod'; | ||
import { getPublishedStory } from '../../../../services/stories'; | ||
|
||
const puppeteer = require("puppeteer") | ||
|
||
export async function POST(req: Request){ | ||
export async function POST(req: Request) { | ||
try { | ||
const session = await auth(); | ||
|
||
if (!session?.user){ | ||
return new NextResponse(JSON.stringify({ | ||
status: 'fail', message: "You are not logged in", | ||
}), { status : 401}) | ||
if (!session?.user) { | ||
return new NextResponse( | ||
JSON.stringify({ status: 'fail', message: 'You are not logged in' }), | ||
{ status: 401 } | ||
); | ||
} | ||
|
||
const input = exportChapterSchema.parse(await req.json()); | ||
|
||
const story = await getPublishedStory(input.id); | ||
|
||
let jsonContent = [] | ||
|
||
story.chapters.forEach((c) => jsonContent.push(...c.blocks)) | ||
|
||
|
||
const content = { | ||
"type": 'doc', | ||
"content": jsonContent | ||
if (!story) { | ||
return new NextResponse( | ||
JSON.stringify({ status: 'fail', message: 'Story not found' }), | ||
{ status: 404 } | ||
); | ||
} | ||
|
||
console.log(content) | ||
|
||
const html = generateHTML( content , [ | ||
StarterKit, | ||
]) | ||
return new NextResponse(JSON.stringify(story), { status: 200 }); | ||
} catch (error) { | ||
console.error('Error fetching story:', error); | ||
|
||
console.log(html) | ||
|
||
|
||
const browser = await puppeteer.launch(); | ||
const page = await browser.newPage(); | ||
await page.setContent(html, {waitUntil: 'networkidle0'}) | ||
const pdfBuffer = await page.pdf({format: 'A4'}) | ||
await browser.close(); | ||
const response = new NextResponse(pdfBuffer); | ||
|
||
response.headers.set("Content-Type", "application/pdf") | ||
console.log("render complete") | ||
|
||
return response; | ||
|
||
}catch (e) { | ||
console.log(e) | ||
return new NextResponse( | ||
JSON.stringify({ status: 'error', message: 'Failed to fetch story' }), | ||
{ status: 500 } | ||
); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.