@dimah-s3/server 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/api/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { createMiddleware, createEndpoint } from 'better-call';
2
2
  import * as z from 'zod';
3
- import { S3_API_ROUTES, isAPIError, APIError, S3_ERROR_CODES, confirmBodySchema, deleteQuerySchema, deleteBatchBodySchema, downloadQuerySchema, fileQuerySchema, multipartAbortBodySchema, multipartCompleteBodySchema, multipartInitBodySchema, multipartListPartsQuerySchema, multipartSignPartBodySchema, uploadBodySchema, buildContentDisposition, S3_MAX_EXPIRES_IN, S3_MAX_POST_OBJECT_BYTES, resolveStoredFileName, S3_DEFAULT_EXPIRES_IN, matchesFileTypes, normalizeObjectKey, sanitizeFileName } from '@dimah-s3/core';
4
- import { DeleteObjectCommand, GetObjectCommand, CreateMultipartUploadCommand, UploadPartCommand, PutObjectCommand, ListPartsCommand, AbortMultipartUploadCommand, CompleteMultipartUploadCommand, HeadObjectCommand } from '@aws-sdk/client-s3';
3
+ import { S3_API_ROUTES, isAPIError, APIError, S3_ERROR_CODES, confirmBodySchema, deleteQuerySchema, deleteBatchBodySchema, downloadQuerySchema, fileQuerySchema, multipartAbortBodySchema, multipartCompleteBodySchema, multipartInitBodySchema, multipartListPartsQuerySchema, multipartSignPartBodySchema, uploadBodySchema, buildContentDisposition, S3_MAX_EXPIRES_IN, S3_MAX_POST_OBJECT_BYTES, resolveStoredFileName, resolveInlineDisposition, fileNameFromKey, S3_DEFAULT_EXPIRES_IN, matchesFileTypes, normalizeObjectKey, sanitizeFileName } from '@dimah-s3/core';
4
+ import { DeleteObjectCommand, GetObjectCommand, CreateMultipartUploadCommand, UploadPartCommand, PutObjectCommand, HeadObjectCommand, ListPartsCommand, AbortMultipartUploadCommand, CompleteMultipartUploadCommand } from '@aws-sdk/client-s3';
5
5
  import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
6
6
  import { createPresignedPost } from '@aws-sdk/s3-presigned-post';
7
7
 
@@ -407,6 +407,15 @@ async function openStoredTarget(config, input, request, feature) {
407
407
  stored: storedObjectContext(request, route, target.key)
408
408
  };
409
409
  }
410
+ async function objectExists(client, bucket, key) {
411
+ try {
412
+ await client.send(new HeadObjectCommand({ Bucket: bucket, Key: key }));
413
+ return true;
414
+ } catch (err) {
415
+ if (isAwsNotFound(err)) return false;
416
+ throw err;
417
+ }
418
+ }
410
419
  async function headObjectOrNotFound(client, bucket, key) {
411
420
  return sendOrObjectNotFound(
412
421
  () => client.send(new HeadObjectCommand({ Bucket: bucket, Key: key }))
@@ -460,6 +469,15 @@ function listedPartsByteSize(parts, excludePartNumber) {
460
469
  }
461
470
  return total;
462
471
  }
472
+ function applyInlineTypes(inlineTypes, input) {
473
+ if (!inlineTypes) return { disposition: input.disposition };
474
+ return resolveInlineDisposition({
475
+ contentType: input.contentType,
476
+ fileName: input.fileName ?? fileNameFromKey(input.key),
477
+ allow: inlineTypes,
478
+ requested: input.disposition
479
+ });
480
+ }
463
481
  async function finalizeConfirmedObject(client, bucket, key, input) {
464
482
  const { config, route, stored, head, uploadId, acl } = input;
465
483
  const rawMetadata = head.Metadata ?? {};
@@ -594,7 +612,7 @@ var confirm = createS3Endpoint(
594
612
  return handleConfirm(ctx.context.config, ctx.body, ctx.context.request);
595
613
  }
596
614
  );
597
- async function handleDelete(config, input, request) {
615
+ async function deleteStoredObject(config, input, request) {
598
616
  const { route, key, bucket, stored } = await openStoredTarget(
599
617
  config,
600
618
  input,
@@ -602,34 +620,30 @@ async function handleDelete(config, input, request) {
602
620
  "delete"
603
621
  );
604
622
  await runHook(route.delete.guard, stored);
605
- await headObjectOrNotFound(route.client, bucket, key);
606
- await route.client.send(
607
- new DeleteObjectCommand({ Bucket: bucket, Key: key })
623
+ const existed = await objectExists(route.client, bucket, key);
624
+ if (existed) {
625
+ await route.client.send(
626
+ new DeleteObjectCommand({ Bucket: bucket, Key: key })
627
+ );
628
+ }
629
+ await runLifecycleHook(
630
+ route.delete.onDeleted,
631
+ { ...stored, existed },
632
+ config
608
633
  );
609
- await runLifecycleHook(route.delete.onDeleted, stored, config);
610
- return { success: true, bucket, key };
634
+ return { success: true, bucket, key, existed };
611
635
  }
612
636
  var deleteObject = createS3Endpoint(
613
637
  S3_API_ROUTES.delete,
614
638
  { method: "DELETE", query: deleteQuerySchema },
615
639
  async (ctx) => {
616
- return handleDelete(ctx.context.config, ctx.query, ctx.context.request);
640
+ return deleteStoredObject(
641
+ ctx.context.config,
642
+ ctx.query,
643
+ ctx.context.request
644
+ );
617
645
  }
618
646
  );
619
- async function deleteOne(config, routeName, key, request) {
620
- const { route, bucket, stored } = await openStoredTarget(
621
- config,
622
- { route: routeName, key },
623
- request,
624
- "delete"
625
- );
626
- await runHook(route.delete.guard, stored);
627
- await headObjectOrNotFound(route.client, bucket, stored.key);
628
- await route.client.send(
629
- new DeleteObjectCommand({ Bucket: bucket, Key: stored.key })
630
- );
631
- await runLifecycleHook(route.delete.onDeleted, stored, config);
632
- }
633
647
  var deleteBatch = createS3Endpoint(
634
648
  S3_API_ROUTES.deleteBatch,
635
649
  { method: "POST", body: deleteBatchBodySchema },
@@ -639,8 +653,12 @@ var deleteBatch = createS3Endpoint(
639
653
  const results = [];
640
654
  for (const key of ctx.body.keys) {
641
655
  try {
642
- await deleteOne(config, ctx.body.route, key, request);
643
- results.push({ key, success: true });
656
+ const { existed } = await deleteStoredObject(
657
+ config,
658
+ { route: ctx.body.route, key },
659
+ request
660
+ );
661
+ results.push({ key, success: true, existed });
644
662
  } catch (err) {
645
663
  results.push({
646
664
  key,
@@ -691,19 +709,26 @@ async function handleDownload(config, input, request) {
691
709
  disposition,
692
710
  expiresIn
693
711
  });
694
- await headObjectOrNotFound(route.client, bucket, key);
695
- const contentDisposition = fileName ? buildContentDisposition(fileName, disposition) : disposition;
712
+ const head = await headObjectOrNotFound(route.client, bucket, key);
713
+ const signed = applyInlineTypes(route.download.inlineTypes, {
714
+ key,
715
+ fileName,
716
+ disposition,
717
+ contentType: head.ContentType
718
+ });
719
+ const contentDisposition = fileName ? buildContentDisposition(fileName, signed.disposition) : signed.disposition;
696
720
  const url = mode === "proxy" ? fileUrl(request, config.basePath ?? "/api/s3", {
697
721
  route: input.route,
698
722
  key,
699
723
  fileName,
700
- disposition
724
+ disposition: signed.disposition
701
725
  }) : await getSignedUrl(
702
726
  route.client,
703
727
  new GetObjectCommand({
704
728
  Bucket: bucket,
705
729
  Key: key,
706
- ResponseContentDisposition: contentDisposition
730
+ ResponseContentDisposition: contentDisposition,
731
+ ResponseContentType: signed.contentType
707
732
  }),
708
733
  { expiresIn }
709
734
  );
@@ -723,7 +748,7 @@ async function handleDownload(config, input, request) {
723
748
  url,
724
749
  expiresIn,
725
750
  mode,
726
- disposition
751
+ disposition: signed.disposition
727
752
  };
728
753
  }
729
754
  var download = createS3Endpoint(
@@ -750,6 +775,7 @@ function catalogDownload(download2, maxExpiresIn) {
750
775
  enabled: true,
751
776
  expiresIn: normalizeExpiresIn(download2.expiresIn, maxExpiresIn),
752
777
  ...download2.disposition ? { disposition: download2.disposition } : {},
778
+ ...download2.inlineTypes ? { inlineTypes: [...download2.inlineTypes] } : {},
753
779
  ...download2.mode ? { mode: download2.mode } : {}
754
780
  };
755
781
  }
@@ -790,13 +816,20 @@ async function handleFile(config, input, request) {
790
816
  fileName,
791
817
  disposition
792
818
  });
793
- await headObjectOrNotFound(route.client, bucket, key);
819
+ const head = await headObjectOrNotFound(route.client, bucket, key);
820
+ const signed = applyInlineTypes(route.download.inlineTypes, {
821
+ key,
822
+ fileName,
823
+ disposition,
824
+ contentType: head.ContentType
825
+ });
826
+ const contentDisposition = fileName ? buildContentDisposition(fileName, signed.disposition) : signed.disposition;
794
827
  const result = await sendOrObjectNotFound(
795
828
  () => route.client.send(
796
829
  new GetObjectCommand({
797
830
  Bucket: bucket,
798
831
  Key: key,
799
- ResponseContentDisposition: fileName ? buildContentDisposition(fileName, disposition) : disposition
832
+ ResponseContentDisposition: contentDisposition
800
833
  })
801
834
  )
802
835
  );
@@ -804,11 +837,12 @@ async function handleFile(config, input, request) {
804
837
  if (!body) throw errors.objectNotFound();
805
838
  const stream = "transformToWebStream" in body && typeof body.transformToWebStream === "function" ? body.transformToWebStream() : body;
806
839
  const headers = new Headers();
807
- headers.set("Content-Type", result.ContentType ?? "application/octet-stream");
808
840
  headers.set(
809
- "Content-Disposition",
810
- fileName ? buildContentDisposition(fileName, disposition) : disposition
841
+ "Content-Type",
842
+ signed.contentType ?? result.ContentType ?? "application/octet-stream"
811
843
  );
844
+ headers.set("Content-Disposition", contentDisposition);
845
+ headers.set("X-Content-Type-Options", "nosniff");
812
846
  if (typeof result.ContentLength === "number") {
813
847
  headers.set("Content-Length", String(result.ContentLength));
814
848
  }