Skip to content

Commit

Permalink
switch to on demand part rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ghackenberg committed Dec 1, 2023
1 parent 64253b6 commit 76370ee
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export class PartController {
async getPart(
@Param('name') name: string
): Promise<StreamableFile> {
return new StreamableFile(await this.service.getPart(name))
return this.service.getPart(name)
}
}
36 changes: 17 additions & 19 deletions packages/backend/scripts/src/modules/rest/parts/part.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ReadStream, createReadStream, createWriteStream, existsSync, mkdirSync, readFileSync, readdirSync, statSync } from "fs"
import { createReadStream, createWriteStream, existsSync, mkdirSync, readFileSync, readdirSync, statSync } from "fs"
import { dirname, join } from "path"

import { Injectable, NotFoundException } from "@nestjs/common"
import { Injectable, NotFoundException, StreamableFile } from "@nestjs/common"

import axios from 'axios'
import { Entry, fromBuffer } from 'yauzl'
Expand All @@ -19,7 +19,7 @@ export class PartService {
private readonly paths: {[name: string]: string} = {}

constructor() {
this.prepare().then(() => this.index(this.LDRAW)).then(() => this.render())
this.prepare().then(() => this.index(this.LDRAW))
}

private async prepare(): Promise<void> {
Expand Down Expand Up @@ -86,32 +86,30 @@ export class PartService {
}
}

private async render() {
console.log(`Rendering`)
if (!existsSync('./previews')) {
mkdirSync('./previews')
}
for (const partName in this.paths) {
if (partName.endsWith('.dat')) {
const imageName = partName.replace('.dat', '.png')
if (!(imageName in this.paths)) {
public async getPart(name: string): Promise<StreamableFile> {
if (!(name in this.paths)) {
if (name.endsWith('.png')) {
const partName = name.replace('.png', '.dat')
if (partName in this.paths) {
console.log(`Rendering ${partName}`)
const partPath = this.paths[partName]
const partData = readFileSync(partPath, 'utf-8')
const imagePath = partPath.replace('.dat', '.png')
const imageData = await renderLDraw(partData, 512, 512)
imageData.write(imagePath)
await imageData.writeAsync(imagePath)
this.paths[name] = imagePath
}
}
}
}

public async getPart(name: string): Promise<ReadStream> {
if (name in this.paths) {
return createReadStream(this.paths[name])
} else {
throw new NotFoundException()
const stream = createReadStream(this.paths[name])
if (name.endsWith('.png')) {
return new StreamableFile(stream, { type: 'image/png', disposition: 'inline' })
} else {
return new StreamableFile(stream)
}
}
throw new NotFoundException()
}

}

0 comments on commit 76370ee

Please sign in to comment.