@theswing/cdn
v1.0.8
Published
The Swing 공용 CDN에 파일 업로드 하는 모듈
Downloads
452
Readme
@theswing/cdn
The Swing 공용 CDN에 파일 업로드 하는 모듈
Installing
Using npm:
$ npm install @theswing/cdn
Example
const { CDNUploader, FileTypes, Utils } = require("@theswing/cdn");
// or
import { CDNUploader, FileTypes, Utils } from "@theswing/cdn";
// ...
const cdnUploader = new CDNUploader("API_KEY");
console.log(cdnUploader.domain);
const r = await cdnUploader.performUploadUI(FileTypes.IMAGE);
console.log(r);
window.location.href = Utils.resizedHeight(r.url, 100);
API
new CDNUploader(apiKey)
- 객체 생성자
apiKey
: (필수) CDN Domain API로 객체 생성
get domain: String
- 프로퍼티
apiKey
토큰의 도메인을 출력
async function upload(data, config): UploadResult
- CDN에 파일 업로드 작업
data
: (필수) 업로드 할 파일, 데이터, BLOB, Buffer 등config
: 설정filename
: (필수) 파일명mime
: (필수) 파일 종류metadata
: (선택) Object 별로로 저장할 데이터 JSON으로 저장됨secure
: (선택) Boolean 보안 영역 저장 여부 (선택시 url 반환 안함)onUploadProgress
: (선택) 업로드 진행률 콜백 함수 (UI 표시시 사용)
async function exists(fileKey): Boolean
- CDN에 올라간 파일인지 확인하는 함수
fileKey
: (필수) 파일 식별자
async performUploadUI(allowedFileTypes[, config]): UploadResult
- CDN에 파일 업로드 작업 (UI 지원)
allowedFileTypes
: 파일 선택 창에서 허용할 파일 종류 (FileTypes
참고)config
: (선택) 설정secure
: (선택) Boolean 보안 영역 저장 여부 (선택시 url 반환 안함)onUploadProgress
: (선택) 업로드 진행률 콜백 함수 (UI 표시시 사용)metadata
: (선택) Object 별로로 저장할 데이터 JSON으로 저장됨
async getMetadata(fileKey)
- CDN에 업로드된 메타데이터 다운로드
fileKey
: (필수) 파일키
async getFile(fileKey)
- CDN에 업로드된 파일 직접 다운로드 후 Blob로 반환 (CDN 캐시 타지 않고 S3에서 직접)
fileKey
: (필수) 파일키
async getFileURL(fileKey)
- CDN에 업로드된 파일 직접 다운로드한 Data URL을 반환 (CDN 캐시 타지 않고 S3에서 직접)
fileKey
: (필수) 파일키
일반적으로 공개 파일은 CDN URL로 대응하고 getFile API는 비공개 파일 가져올때 활용
Definition
UploadResult
url
: CDN URL (secure == true인 경우 제외)fileKey
: 파일 식별자
FileTypes
- MIME 스펙이면 직접 입력 가능
- https://developer.mozilla.org/ko/docs/Web/HTTP/Basics_of_HTTP/MIME_types
Utils
isCDNURL(url): Boolean
- URL이 CDN URL인지 판정
resizedWidth(url, width): String
- CDN 이미지 URL을 넓이 기준으로 리사이즈된 URL을 반환 (비율 유지)
resizedHeight(url, height): String
- CDN 이미지 URL을 높이 기준으로 리사이즈된 URL을 반환 (비율 유지)