Problem: Standardize File Storage Clients
Problem statement
Your backend uses two file storage systems:
A local file system client for development, which saves files directly on disk.
A mock S3 client in production, which uploads files to a cloud bucket.
Both expose similar functionality but with different method names and behaviors:
The local client uses
save(file)andread(file).The S3-like client uses
uploadFile(file)andgetFile(file).
This mismatch makes your file-handling code messy and repetitive. You need one consistent interface for both.
Goal
Implement a StorageAdapter class that wraps either client and exposes these two methods:
.upload(file).download(file)
Each method should internally call the appropriate function from the wrapped client and return its result.
Constraints
Do not modify the original clients.
The adapter must detect which client it’s wrapping.
The calling code should always use
.upload()and.download().No hardcoded conditionals like
if (env === 'dev'); use method detection instead.
Sample output
The examples below illustrate what the output should look like:
const localStorage = new StorageAdapter(localFs);const cloudStorage = new StorageAdapter(s3Client);console.log(localStorage.upload('report.pdf'));console.log(localStorage.download('report.pdf'));console.log(cloudStorage.upload('report.pdf'));console.log(cloudStorage.download('report.pdf'));/* Expected output:Saved report.pdf locallyRead report.pdf from local diskUploaded report.pdf to S3Fetched report.pdf from S3*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.
Problem: Standardize File Storage Clients
Problem statement
Your backend uses two file storage systems:
A local file system client for development, which saves files directly on disk.
A mock S3 client in production, which uploads files to a cloud bucket.
Both expose similar functionality but with different method names and behaviors:
The local client uses
save(file)andread(file).The S3-like client uses
uploadFile(file)andgetFile(file).
This mismatch makes your file-handling code messy and repetitive. You need one consistent interface for both.
Goal
Implement a StorageAdapter class that wraps either client and exposes these two methods:
.upload(file).download(file)
Each method should internally call the appropriate function from the wrapped client and return its result.
Constraints
Do not modify the original clients.
The adapter must detect which client it’s wrapping.
The calling code should always use
.upload()and.download().No hardcoded conditionals like
if (env === 'dev'); use method detection instead.
Sample output
The examples below illustrate what the output should look like:
const localStorage = new StorageAdapter(localFs);const cloudStorage = new StorageAdapter(s3Client);console.log(localStorage.upload('report.pdf'));console.log(localStorage.download('report.pdf'));console.log(cloudStorage.upload('report.pdf'));console.log(cloudStorage.download('report.pdf'));/* Expected output:Saved report.pdf locallyRead report.pdf from local diskUploaded report.pdf to S3Fetched report.pdf from S3*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.