Skip to content

Commit

Permalink
Fix asset upload for local
Browse files Browse the repository at this point in the history
  • Loading branch information
miku448 committed Sep 29, 2024
1 parent a92d3f4 commit 5dce42c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 27 deletions.
15 changes: 6 additions & 9 deletions .env.default
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
# browser-directory env
VITE_SERVICES_ENDPOINT=http://localhost:8484
VITE_BOT_DIRECTORY_ENDPOINT=http://localhost:8585/bots
VITE_IMAGES_DIRECTORY_ENDPOINT=http://localhost:8585/image
VITE_CARD_ENDPOINT=http://localhost:8585/s3/bots
VITE_ASSETS_ENDPOINT=http://localhost:8585/s3/assets
INTERACTOR_ENDPOINT=http://localhost:5173

# services env
AZURE_API_KEY=<AZURE_API_KEY>
AZURE_API_KEY=
SERVICES_PORT=8484

# bot-directory env
BOT_DIRECTORY_PORT=8585
BOT_DIRECTORY_PORT=8585
VITE_ASSETS_UPLOAD_URL=http://localhost:8585/asset-upload
VITE_STAGE=development
15 changes: 6 additions & 9 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
# browser-directory env
VITE_SERVICES_ENDPOINT=http://localhost:8484
VITE_BOT_DIRECTORY_ENDPOINT=http://localhost:8585/bots
VITE_IMAGES_DIRECTORY_ENDPOINT=http://localhost:8585/image
VITE_CARD_ENDPOINT=http://localhost:8585/s3/bots
VITE_ASSETS_ENDPOINT=http://localhost:8585/s3/assets
INTERACTOR_ENDPOINT=http://localhost:5173

# services env
AZURE_API_KEY=<AZURE_API_KEY>
AZURE_API_KEY=
SERVICES_PORT=8484

# bot-directory env
BOT_DIRECTORY_PORT=8585
BOT_DIRECTORY_PORT=8585
VITE_ASSETS_UPLOAD_URL=http://localhost:8585/asset-upload
VITE_STAGE=development
16 changes: 16 additions & 0 deletions apps/bot-directory/src/s3server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,20 @@ export default function s3ServerDecorator(app: Express): void {
});

app.post('/asset-upload/complete', uploadAssetHandler);

app.post('/asset-upload', upload.single('file'), async (req, res) => {
if (!req.file) {
return res.status(400).send('No file uploaded.');
}
const fileName = req.file.originalname;
const filePath = path.join(path.join(dataDir, `assets/${fileName}`));
// @ts-ignore
fs.writeFile(filePath, req.file.buffer, (err) => {
if (err) {
console.error('Error writing file:', err);
return res.status(500).send('Error writing file.');
}
res.status(200).send(fileName);
});
});
}
14 changes: 13 additions & 1 deletion apps/bot-directory/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ const app = express();
app.use('/public', express.static(process.cwd() + '/public'));
app.set('view engine', 'ejs');

app.use(cors());
app.use(
cors({
origin: [
'http://localhost:5173',
'http://localhost:5174',
'http://localhost:8586',
'http://localhost:8587',
'http://localhost:8585',
'http://localhost:8484',
],
credentials: true,
}),
);
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
app.use(bodyParser.json({ limit: '50mb' }));
app.use(
Expand Down
17 changes: 9 additions & 8 deletions apps/novel-builder/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { BackgroundResult, listSearch, SearchType, SongResult } from './libs/listSearch';

export const MAX_FILE_SIZE = 5 * 1024 * 1024;
const VITE_ASSETS_UPLOAD_URL = import.meta.env.VITE_ASSETS_UPLOAD_URL || 'http://localhost:8585/asset-upload';

interface BuilderConfig {
assetsEndpoint: string;
Expand Down Expand Up @@ -69,15 +70,15 @@ const configs: Map<'development' | 'staging' | 'production', BuilderConfig> = ne
if (typeof file === 'string') {
file = await dataURLtoFile(file, 'asset');
}
if (file.type.startsWith('image')) {
file = new File([await convertToWebP(file)], 'asset.webp', { type: 'image/webp' });
}

const result = await fetch('http://localhost:8585/asset-upload', {
method: 'POST',
body: file,
}).then((res) => res.json());
const result = await uploadAsset(VITE_ASSETS_UPLOAD_URL, file, type);

return {
success: !!result.fileName,
assetId: result.fileName,
success: result.status === 200 || result.status === 201,
assetId: result.data,
};
},
previewIframe: 'http://localhost:5173',
Expand Down Expand Up @@ -179,7 +180,7 @@ const configs: Map<'development' | 'staging' | 'production', BuilderConfig> = ne
file = new File([await convertToWebP(file)], 'asset.webp', { type: 'image/webp' });
}

const result = await uploadAsset(import.meta.env.VITE_ASSETS_UPLOAD_URL, file, type);
const result = await uploadAsset(VITE_ASSETS_UPLOAD_URL, file, type);

return {
success: result.status === 200 || result.status === 201,
Expand Down Expand Up @@ -275,7 +276,7 @@ const configs: Map<'development' | 'staging' | 'production', BuilderConfig> = ne
file = new File([await convertToWebP(file)], 'asset.webp', { type: 'image/webp' });
}

const result = await uploadAsset(import.meta.env.VITE_ASSETS_UPLOAD_URL, file, type);
const result = await uploadAsset(VITE_ASSETS_UPLOAD_URL, file, type);

return {
success: result.status === 200 || result.status === 201,
Expand Down

0 comments on commit 5dce42c

Please sign in to comment.