AdminJS에서 Content Type 추가하기

rxolve·2024년 3월 5일
post-thumbnail

AdminJS

AdminJS는 백오피스를 편하게 구성해준다. ORM의 엔티티만 추가해도 CRUD가 가능하다.

문제

파일 업로드를 위해 uploadFileFeature 함수를 지원한다. 하지만 mimeType 프로퍼티를 설정해도 S3에 업로드된 파일의 Content-Type은 application/octet-stream으로 설정되어 브라우저에서 열리지 않는다. 다운로드만 될 뿐.

https://adminjs-docs.web.app/module-@adminjs_upload.html

해결

  1. 이미지 업로드 후 실행되는 함수를 설정한다.
actions: {
  new: {
    after: async (response: ActionResponse) => {
      await updateImageData(response);
      return response;
    },
  },
  edit: {
    after: async (response: ActionResponse) => {
      await updateImageData(response);
      return response;
    },
  },
},
  1. 앞의 함수에서 s3 객체를 만들고 업로드된 이미지를 복사하며 ContentType을 수정한다.
s3.copyObject({
  Bucket: bucket,
  Key: fileName,
  CopySource: `${bucket}/${fileName}`,
  ContentType: '여기서 설정',
  MetadataDirective: 'REPLACE',
})
  .then((data) => console.log(data))
  .catch((err) => console.log(err));

마음에 드는 해결책은 아니지만 원활한 운영을 위해 커밋했다. 깃헙에 이슈를 제보하려는데 이미 올라와 있더라. 4년전에.

https://github.com/SoftwareBrothers/adminjs-upload/issues/37

관리가 더 잘되는 프로젝트였으면...

profile
resolve to solve. 해결할 결심.

0개의 댓글