Skip to content

Commit

Permalink
StaticDatasets: adapt upload for storing geojsons
Browse files Browse the repository at this point in the history
  • Loading branch information
elsueno committed Jun 5, 2024
1 parent 7a239bd commit 361cc84
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- CreateEnum
CREATE TYPE "UploadCreatedByEnum" AS ENUM ('USER', 'SCRIPT');

-- CreateEnum
CREATE TYPE "UploadTypeEnum" AS ENUM ('GEOJSON', 'PMTILES');

-- AlterTable
ALTER TABLE "Upload"
RENAME COLUMN "pmtilesUrl" TO "url";
ALTER TABLE "Upload"
ADD COLUMN "createdBy" "UploadCreatedByEnum" NOT NULL DEFAULT 'SCRIPT',
ADD COLUMN "type" "UploadTypeEnum" NOT NULL DEFAULT 'PMTILES';
28 changes: 20 additions & 8 deletions db/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,26 @@ model Membership {
}

model Upload {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
createdBy UploadCreatedByEnum @default(SCRIPT)
updatedAt DateTime @updatedAt
//
slug String @unique
pmtilesUrl String
configs Json
public Boolean @default(false)
slug String @unique
type UploadTypeEnum @default(PMTILES)
url String
configs Json
public Boolean @default(false)
//
regions Region[]
regions Region[]
}

enum UploadCreatedByEnum {
USER
SCRIPT
}

enum UploadTypeEnum {
GEOJSON
PMTILES
}
4 changes: 2 additions & 2 deletions db/seeds/pmtiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const seedUploads = async () => {
const seedUploadsNudafa: Prisma.UploadUncheckedCreateInput[] = [
{
slug: 'nudafa-combined',
pmtilesUrl:
url:
'https://atlas-private.s3.eu-central-1.amazonaws.com/test-data/nudafa-combined.pmtiles',
configs: [
{
Expand All @@ -39,7 +39,7 @@ const seedUploads = async () => {
const seedUploadsBibi: Prisma.UploadUncheckedCreateInput[] = [
{
slug: 'two-configs',
pmtilesUrl:
url:
'https://atlas-private.s3.eu-central-1.amazonaws.com/test-data/nudafa-combined.pmtiles',
configs: [
{
Expand Down
2 changes: 1 addition & 1 deletion scripts/StaticDatasets/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const getRegions = async (): Promise<{ id: number; slug: string }[]> => {

type UploadData = {
uploadSlug: string
pmtilesUrl: string
url: string
regionSlugs: string[]
isPublic: boolean
configs: Record<string, any>[]
Expand Down
2 changes: 1 addition & 1 deletion scripts/StaticDatasets/updateStaticDatasets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ for (const { datasetFolderPath, regionFolder, datasetFolder } of datasetFileFold
})
await createUpload({
uploadSlug,
pmtilesUrl,
url: pmtilesUrl,
regionSlugs,
isPublic: metaData.public,
configs: mergedConfigs,
Expand Down
21 changes: 16 additions & 5 deletions src/app/admin/uploads/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,26 @@ export default function AdminUploadPage() {
</p>

<p>
Vorschau für Devs (<code>_API_KEY_</code> aus <code>.env</code>/Bitwarden für{' '}
<code>{process.env.NEXT_PUBLIC_APP_ENV}</code>)
<textarea value={previewUrl.toString()} className="w-full text-sm" />
Datentyp: <code>{upload.type}</code>
</p>

{upload.type === 'GEOJSON' ? (
<p>
<b>TODO: add link to geojson viewer</b>
</p>
) : (
<p>
Vorschau für Devs (<code>_API_KEY_</code> aus <code>.env</code>/Bitwarden für{' '}
<code>{process.env.NEXT_PUBLIC_APP_ENV}</code>)
<textarea value={previewUrl.toString()} className="w-full text-sm" />
</p>
)}

<p>
Öffentliche URL <code>{getStaticDatasetUrl(upload.slug)}</code>
Öffentliche URL: <code>{getStaticDatasetUrl(upload.slug)}</code>
</p>
<p>
Interne URL auf S3 <code>{upload.pmtilesUrl}</code>
Interne URL: <code>{upload.url}</code>
</p>

{configs.map((config) => {
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/uploads/[slug]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function GET(request: Request, { params }: { params: { slug: string
}
}

const { hostname, pathname } = new URL(upload!.pmtilesUrl)
const { hostname, pathname } = new URL(upload!.url)
const accessKeyId = process.env.S3_KEY!
const secretAccessKey = process.env.S3_SECRET!
const region = process.env.S3_REGION!
Expand Down
6 changes: 3 additions & 3 deletions src/app/api/uploads/create/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { parseData, checkApiKey } from '../../_util/checkApiKey'
const Schema = z.object({
apiKey: z.string().nullish(),
uploadSlug: z.string(),
pmtilesUrl: z.string(),
url: z.string(),
regionSlugs: z.array(z.string()),
isPublic: z.boolean(),
configs: z.array(z.record(z.string(), z.any())),
Expand All @@ -19,15 +19,15 @@ export async function POST(request: Request) {
const check = checkApiKey(data)
if (!check.ok) return check.errorResponse

const { uploadSlug, pmtilesUrl, regionSlugs, isPublic, configs } = data
const { uploadSlug, url, regionSlugs, isPublic, configs } = data

await db.upload.deleteMany({ where: { slug: uploadSlug } })

try {
await db.upload.create({
data: {
slug: uploadSlug,
pmtilesUrl,
url,
regions: { connect: regionSlugs.map((slug) => ({ slug })) },
public: isPublic,
configs,
Expand Down
2 changes: 1 addition & 1 deletion src/uploads/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RegionSchema } from '../regions/schemas'
export const UploadSchema = z.object({
id: z.number(),
slug: z.string(),
pmtilesUrl: z.string().url(),
url: z.string().url(),
public: z.boolean(),
})

Expand Down

0 comments on commit 361cc84

Please sign in to comment.