@dimah-s3/server 1.6.0 → 1.7.1

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/plugins.js CHANGED
@@ -1,5 +1,5 @@
1
- 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, ROUTE_NAME_PATTERN, normalizeObjectKey, sanitizeFileName } from '@dimah-s3/core';
2
- import { DeleteObjectCommand, GetObjectCommand, CreateMultipartUploadCommand, UploadPartCommand, PutObjectCommand, ListPartsCommand, AbortMultipartUploadCommand, CompleteMultipartUploadCommand, HeadObjectCommand } from '@aws-sdk/client-s3';
1
+ 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, ROUTE_NAME_PATTERN, normalizeObjectKey, sanitizeFileName } from '@dimah-s3/core';
2
+ import { DeleteObjectCommand, GetObjectCommand, CreateMultipartUploadCommand, UploadPartCommand, PutObjectCommand, HeadObjectCommand, ListPartsCommand, AbortMultipartUploadCommand, CompleteMultipartUploadCommand } from '@aws-sdk/client-s3';
3
3
  import { createMiddleware, createEndpoint } from 'better-call';
4
4
  import * as z from 'zod';
5
5
  import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
@@ -535,6 +535,15 @@ async function openStoredTarget(config, input, request, feature) {
535
535
  stored: storedObjectContext(request, route, target.key)
536
536
  };
537
537
  }
538
+ async function objectExists(client, bucket, key) {
539
+ try {
540
+ await client.send(new HeadObjectCommand({ Bucket: bucket, Key: key }));
541
+ return true;
542
+ } catch (err) {
543
+ if (isAwsNotFound(err)) return false;
544
+ throw err;
545
+ }
546
+ }
538
547
  async function headObjectOrNotFound(client, bucket, key) {
539
548
  return sendOrObjectNotFound(
540
549
  () => client.send(new HeadObjectCommand({ Bucket: bucket, Key: key }))
@@ -588,6 +597,15 @@ function listedPartsByteSize(parts, excludePartNumber) {
588
597
  }
589
598
  return total;
590
599
  }
600
+ function applyInlineTypes(inlineTypes, input) {
601
+ if (!inlineTypes) return { disposition: input.disposition };
602
+ return resolveInlineDisposition({
603
+ contentType: input.contentType,
604
+ fileName: input.fileName ?? fileNameFromKey(input.key),
605
+ allow: inlineTypes,
606
+ requested: input.disposition
607
+ });
608
+ }
591
609
  async function finalizeConfirmedObject(client, bucket, key, input) {
592
610
  const { config, route, stored, head, uploadId, acl } = input;
593
611
  const rawMetadata = head.Metadata ?? {};
@@ -722,7 +740,7 @@ var confirm = createS3Endpoint(
722
740
  return handleConfirm(ctx.context.config, ctx.body, ctx.context.request);
723
741
  }
724
742
  );
725
- async function handleDelete(config, input, request) {
743
+ async function deleteStoredObject(config, input, request) {
726
744
  const { route, key, bucket, stored } = await openStoredTarget(
727
745
  config,
728
746
  input,
@@ -730,34 +748,30 @@ async function handleDelete(config, input, request) {
730
748
  "delete"
731
749
  );
732
750
  await runHook(route.delete.guard, stored);
733
- await headObjectOrNotFound(route.client, bucket, key);
734
- await route.client.send(
735
- new DeleteObjectCommand({ Bucket: bucket, Key: key })
751
+ const existed = await objectExists(route.client, bucket, key);
752
+ if (existed) {
753
+ await route.client.send(
754
+ new DeleteObjectCommand({ Bucket: bucket, Key: key })
755
+ );
756
+ }
757
+ await runLifecycleHook(
758
+ route.delete.onDeleted,
759
+ { ...stored, existed },
760
+ config
736
761
  );
737
- await runLifecycleHook(route.delete.onDeleted, stored, config);
738
- return { success: true, bucket, key };
762
+ return { success: true, bucket, key, existed };
739
763
  }
740
764
  var deleteObject = createS3Endpoint(
741
765
  S3_API_ROUTES.delete,
742
766
  { method: "DELETE", query: deleteQuerySchema },
743
767
  async (ctx) => {
744
- return handleDelete(ctx.context.config, ctx.query, ctx.context.request);
768
+ return deleteStoredObject(
769
+ ctx.context.config,
770
+ ctx.query,
771
+ ctx.context.request
772
+ );
745
773
  }
746
774
  );
747
- async function deleteOne(config, routeName, key, request) {
748
- const { route, bucket, stored } = await openStoredTarget(
749
- config,
750
- { route: routeName, key },
751
- request,
752
- "delete"
753
- );
754
- await runHook(route.delete.guard, stored);
755
- await headObjectOrNotFound(route.client, bucket, stored.key);
756
- await route.client.send(
757
- new DeleteObjectCommand({ Bucket: bucket, Key: stored.key })
758
- );
759
- await runLifecycleHook(route.delete.onDeleted, stored, config);
760
- }
761
775
  var deleteBatch = createS3Endpoint(
762
776
  S3_API_ROUTES.deleteBatch,
763
777
  { method: "POST", body: deleteBatchBodySchema },
@@ -767,8 +781,12 @@ var deleteBatch = createS3Endpoint(
767
781
  const results = [];
768
782
  for (const key of ctx.body.keys) {
769
783
  try {
770
- await deleteOne(config, ctx.body.route, key, request);
771
- results.push({ key, success: true });
784
+ const { existed } = await deleteStoredObject(
785
+ config,
786
+ { route: ctx.body.route, key },
787
+ request
788
+ );
789
+ results.push({ key, success: true, existed });
772
790
  } catch (err) {
773
791
  results.push({
774
792
  key,
@@ -819,19 +837,26 @@ async function handleDownload(config, input, request) {
819
837
  disposition,
820
838
  expiresIn
821
839
  });
822
- await headObjectOrNotFound(route.client, bucket, key);
823
- const contentDisposition = fileName ? buildContentDisposition(fileName, disposition) : disposition;
840
+ const head = await headObjectOrNotFound(route.client, bucket, key);
841
+ const signed = applyInlineTypes(route.download.inlineTypes, {
842
+ key,
843
+ fileName,
844
+ disposition,
845
+ contentType: head.ContentType
846
+ });
847
+ const contentDisposition = fileName ? buildContentDisposition(fileName, signed.disposition) : signed.disposition;
824
848
  const url = mode === "proxy" ? fileUrl(request, config.basePath ?? "/api/s3", {
825
849
  route: input.route,
826
850
  key,
827
851
  fileName,
828
- disposition
852
+ disposition: signed.disposition
829
853
  }) : await getSignedUrl(
830
854
  route.client,
831
855
  new GetObjectCommand({
832
856
  Bucket: bucket,
833
857
  Key: key,
834
- ResponseContentDisposition: contentDisposition
858
+ ResponseContentDisposition: contentDisposition,
859
+ ResponseContentType: signed.contentType
835
860
  }),
836
861
  { expiresIn }
837
862
  );
@@ -851,7 +876,7 @@ async function handleDownload(config, input, request) {
851
876
  url,
852
877
  expiresIn,
853
878
  mode,
854
- disposition
879
+ disposition: signed.disposition
855
880
  };
856
881
  }
857
882
  var download = createS3Endpoint(
@@ -878,6 +903,7 @@ function catalogDownload(download2, maxExpiresIn) {
878
903
  enabled: true,
879
904
  expiresIn: normalizeExpiresIn(download2.expiresIn, maxExpiresIn),
880
905
  ...download2.disposition ? { disposition: download2.disposition } : {},
906
+ ...download2.inlineTypes ? { inlineTypes: [...download2.inlineTypes] } : {},
881
907
  ...download2.mode ? { mode: download2.mode } : {}
882
908
  };
883
909
  }
@@ -918,13 +944,20 @@ async function handleFile(config, input, request) {
918
944
  fileName,
919
945
  disposition
920
946
  });
921
- await headObjectOrNotFound(route.client, bucket, key);
947
+ const head = await headObjectOrNotFound(route.client, bucket, key);
948
+ const signed = applyInlineTypes(route.download.inlineTypes, {
949
+ key,
950
+ fileName,
951
+ disposition,
952
+ contentType: head.ContentType
953
+ });
954
+ const contentDisposition = fileName ? buildContentDisposition(fileName, signed.disposition) : signed.disposition;
922
955
  const result = await sendOrObjectNotFound(
923
956
  () => route.client.send(
924
957
  new GetObjectCommand({
925
958
  Bucket: bucket,
926
959
  Key: key,
927
- ResponseContentDisposition: fileName ? buildContentDisposition(fileName, disposition) : disposition
960
+ ResponseContentDisposition: contentDisposition
928
961
  })
929
962
  )
930
963
  );
@@ -932,11 +965,12 @@ async function handleFile(config, input, request) {
932
965
  if (!body) throw errors.objectNotFound();
933
966
  const stream = "transformToWebStream" in body && typeof body.transformToWebStream === "function" ? body.transformToWebStream() : body;
934
967
  const headers = new Headers();
935
- headers.set("Content-Type", result.ContentType ?? "application/octet-stream");
936
968
  headers.set(
937
- "Content-Disposition",
938
- fileName ? buildContentDisposition(fileName, disposition) : disposition
969
+ "Content-Type",
970
+ signed.contentType ?? result.ContentType ?? "application/octet-stream"
939
971
  );
972
+ headers.set("Content-Disposition", contentDisposition);
973
+ headers.set("X-Content-Type-Options", "nosniff");
940
974
  if (typeof result.ContentLength === "number") {
941
975
  headers.set("Content-Length", String(result.ContentLength));
942
976
  }