Grouped all file-upload functionality. (#170)

* Grouped all file-upload functionality.

* fix
This commit is contained in:
Martin Šošić
2024-07-02 14:36:25 +02:00
committed by GitHub
parent 25ffc25a0b
commit f01d2414da
10 changed files with 115 additions and 118 deletions

View File

@@ -1,4 +1,4 @@
import { type User, type Task, type File } from 'wasp/entities';
import { type User, type Task } from 'wasp/entities';
import { HttpError } from 'wasp/server';
import {
type GenerateGptResponse,
@@ -8,13 +8,11 @@ import {
type CreateTask,
type DeleteTask,
type UpdateTask,
type CreateFile,
} from 'wasp/server/operations';
import Stripe from 'stripe';
import type { GeneratedSchedule, StripePaymentResult } from '../shared/types';
import { fetchStripeCustomer, createStripeCheckoutSession } from './payments/stripeUtils.js';
import { TierIds } from '../shared/constants.js';
import { getUploadFileSignedURLFromS3 } from './file-upload/s3Utils.js';
import OpenAI from 'openai';
const openai = setupOpenAI();
@@ -308,31 +306,6 @@ export const updateUserById: UpdateUserById<{ id: string; data: Partial<User> },
return updatedUser;
};
type FileDescription = {
fileType: string;
name: string;
};
export const createFile: CreateFile<FileDescription, File> = async ({ fileType, name }, context) => {
if (!context.user) {
throw new HttpError(401);
}
const userInfo = context.user.id;
const { uploadUrl, key } = await getUploadFileSignedURLFromS3({ fileType, userInfo });
return await context.entities.File.create({
data: {
name,
key,
uploadUrl,
type: fileType,
user: { connect: { id: context.user.id } },
},
});
};
export const updateCurrentUser: UpdateCurrentUser<Partial<User>, User> = async (user, context) => {
if (!context.user) {
throw new HttpError(401);

View File

@@ -1,39 +0,0 @@
import { randomUUID } from 'crypto';
import { S3Client } from '@aws-sdk/client-s3';
import { GetObjectCommand, PutObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
const s3Client = new S3Client({
region: process.env.AWS_S3_REGION,
credentials: {
accessKeyId: process.env.AWS_S3_IAM_ACCESS_KEY!,
secretAccessKey: process.env.AWS_S3_IAM_SECRET_KEY!,
},
});
type S3Upload = {
fileType: string;
userInfo: string;
}
export const getUploadFileSignedURLFromS3 = async ({fileType, userInfo}: S3Upload) => {
const ex = fileType.split('/')[1];
const Key = `${userInfo}/${randomUUID()}.${ex}`;
const s3Params = {
Bucket: process.env.AWS_S3_FILES_BUCKET,
Key,
ContentType: `${fileType}`,
};
const command = new PutObjectCommand(s3Params);
const uploadUrl = await getSignedUrl(s3Client, command, { expiresIn: 3600,});
return { uploadUrl, key: Key };
}
export const getDownloadFileSignedURLFromS3 = async ({ key }: { key: string }) => {
const s3Params = {
Bucket: process.env.AWS_S3_FILES_BUCKET,
Key: key,
};
const command = new GetObjectCommand(s3Params);
return await getSignedUrl(s3Client, command, { expiresIn: 3600 });
}

View File

@@ -1,14 +1,11 @@
import { type DailyStats, type GptResponse, type User, type PageViewSource, type Task, type File } from 'wasp/entities';
import { type DailyStats, type GptResponse, type User, type PageViewSource, type Task } from 'wasp/entities';
import { HttpError } from 'wasp/server';
import {
type GetGptResponses,
type GetDailyStats,
type GetPaginatedUsers,
type GetAllTasksByUser,
type GetAllFilesByUser,
type GetDownloadFileSignedURL,
} from 'wasp/server/operations';
import { getDownloadFileSignedURLFromS3 } from './file-upload/s3Utils.js';
import { type SubscriptionStatusOptions } from '../shared/types.js';
type DailyStatsWithSources = DailyStats & {
@@ -49,29 +46,6 @@ export const getAllTasksByUser: GetAllTasksByUser<void, Task[]> = async (_args,
});
};
export const getAllFilesByUser: GetAllFilesByUser<void, File[]> = async (_args, context) => {
if (!context.user) {
throw new HttpError(401);
}
return context.entities.File.findMany({
where: {
user: {
id: context.user.id,
},
},
orderBy: {
createdAt: 'desc',
},
});
};
export const getDownloadFileSignedURL: GetDownloadFileSignedURL<{ key: string }, string> = async (
{ key },
_context
) => {
return await getDownloadFileSignedURLFromS3({ key });
};
export const getDailyStats: GetDailyStats<void, DailyStatsValues> = async (_args, context) => {
if (!context.user?.isAdmin) {
throw new HttpError(401);