Skip to content

Commit

Permalink
Merge pull request #554 from refly-ai/fix/share-publish-errors
Browse files Browse the repository at this point in the history
fix(api): properly handle edges cases for sharing and duplication
  • Loading branch information
mrcfps authored Mar 5, 2025
2 parents 5686c36 + 623fdd0 commit 009ede9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
22 changes: 14 additions & 8 deletions apps/api/src/common/elasticsearch.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,13 @@ export class ElasticsearchService implements OnModuleInit {

async duplicateResource(resourceId: string, newId: string, user: User): Promise<void> {
try {
const { body } = await this.client.get<{ _source: ResourceDocument }>({
index: indexConfig.resource.index,
id: resourceId,
});
const { body } = await this.client.get<{ _source: ResourceDocument }>(
{
index: indexConfig.resource.index,
id: resourceId,
},
{ ignore: [404] },
);

if (!body?._source) {
this.logger.warn(`Resource ${resourceId} not found`);
Expand All @@ -253,10 +256,13 @@ export class ElasticsearchService implements OnModuleInit {

async duplicateDocument(documentId: string, newId: string, user: User): Promise<void> {
try {
const { body } = await this.client.get<{ _source: DocumentDocument }>({
index: indexConfig.document.index,
id: documentId,
});
const { body } = await this.client.get<{ _source: DocumentDocument }>(
{
index: indexConfig.document.index,
id: documentId,
},
{ ignore: [404] },
);

if (!body?._source) {
this.logger.warn(`Document ${documentId} not found`);
Expand Down
3 changes: 3 additions & 0 deletions apps/api/src/misc/misc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ export class MiscService implements OnModuleInit {
* @param storageKey - The storage key of the file to publish
*/
async publishFile(storageKey: string) {
if (!storageKey) {
return '';
}
const stream = await this.minioClient('private').getObject(storageKey);
await this.minioClient('public').putObject(storageKey, stream);
return this.generateFileURL({ visibility: 'public', storageKey });
Expand Down
7 changes: 5 additions & 2 deletions apps/api/src/share/share.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,10 @@ export class ShareService {
}

// Publish minimap
const minimapUrl = await this.miscService.publishFile(canvas.minimapStorageKey);
canvasData.minimapUrl = minimapUrl;
if (canvas.minimapStorageKey) {
const minimapUrl = await this.miscService.publishFile(canvas.minimapStorageKey);
canvasData.minimapUrl = minimapUrl;
}

// Upload public canvas data to Minio
const { storageKey } = await this.miscService.uploadBuffer(user, {
Expand Down Expand Up @@ -310,6 +312,7 @@ export class ShareService {
if (!originalUrl.startsWith(privateStaticEndpoint)) return null;

const storageKey = originalUrl.replace(`${privateStaticEndpoint}/`, '');
if (!storageKey) return null;

try {
// Publish the file to public bucket
Expand Down

0 comments on commit 009ede9

Please sign in to comment.