Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
TimMikeladze committed Nov 30, 2023
1 parent c2c9a3c commit 375ffdd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 67 deletions.
2 changes: 1 addition & 1 deletion examples/next-upload-example/src/app/upload/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export const POST = (request: NextRequest) => nup.handler(request);

export const dynamic = 'force-dynamic';

// export const runtime = 'edge';
export const runtime = 'edge';
78 changes: 12 additions & 66 deletions src/NextUpload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import bytes from 'bytes';
// import { Client, PostPolicy, BucketItem } from 'minio';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { nanoid } from 'nanoid';
import { NextTool, NextToolStorePromise } from 'next-tool';
Expand Down Expand Up @@ -29,6 +28,7 @@ import {
DeleteArgs as DeleteAssetArgs,
SignedPostPolicy,
} from './types';
import { FileReader } from './polyfills/FileReader';

export const defaultEnabledHandlerActions = [
NextUploadAction.generatePresignedPostPolicy,
Expand Down Expand Up @@ -142,63 +142,21 @@ export class NextUpload extends NextTool<NextUploadConfig, NextUploadStore> {
Bucket: this.bucket,
});

function PolyfillFileReader() {
// @ts-ignore
this.result = null;
// @ts-ignore
this.error = null;
// @ts-ignore
this.onload = null;
// @ts-ignore
this.onerror = null;
// @ts-ignore
this.onloadstart = null;
// @ts-ignore
this.onloadend = null;
// @ts-ignore
this.onprogress = null;
// @ts-ignore
this.readyState = 0;
// @ts-ignore
this.abort = function () {
// You can implement abort logic if needed
};
// @ts-ignore
this.readAsText = function (file) {
var reader = this;
var readerEvent = new Event('load');

reader.result = null;
reader.error = null;
reader.readyState = 1; // LOADING

// Simulate asynchronous reading
setTimeout(function () {
// Simulate successful reading
reader.result = 'Contents of the file: ' + file.name;
reader.readyState = 2; // DONE
if (reader.onload) {
reader.onload(readerEvent);
}
reader.onloadend && reader.onloadend(readerEvent);
}, 1000);
};
// @ts-ignore
this.readAsDataURL = function (file) {
// Use the extension from the filename to determine the MIME-TYPE
this.readAsText(file);
};
}

// Assign the polyfilled FileReader to the global scope
// THIS IS A TOTAL HACK!!!
// AWS SDK v3 HeadBucketCommand does not support Edge runtime.
// Assign the polyfilled FileReader to the global scope.
// @ts-ignore
// globalThis.FileReader = PolyfillFileReader;
if (typeof EdgeRuntime === 'string') {
if (!globalThis.FileReader) {
// @ts-ignore
globalThis.FileReader = FileReader;
}
}

try {
console.log(await this.client.send(headBucketCommand));
await this.client.send(headBucketCommand);
return true;
} catch (error) {
console.log(error);
return false;
}
}
Expand Down Expand Up @@ -238,10 +196,7 @@ export class NextUpload extends NextTool<NextUploadConfig, NextUploadStore> {
Bucket: this.bucket,
Key: config.path,
Expires: postPolicyExpirationSeconds,
Conditions: [
['content-length-range', 0, maxSizeBytes],
['eq', '$Content-Type', 'image/jpeg'],
],
Conditions: [['content-length-range', 0, maxSizeBytes]],
};

return postPolicyOptions;
Expand Down Expand Up @@ -397,15 +352,6 @@ export class NextUpload extends NextTool<NextUploadConfig, NextUploadStore> {
verifyAssets ? verifyAssetsExpirationSeconds * 1000 : 0
);

console.log({
postPolicy: {
id,
data: presignedPostPolicy.fields,
url: presignedPostPolicy.url,
path: includeObjectPathInPostPolicyResponse ? path : null,
},
});

return {
postPolicy: {
id,
Expand Down
18 changes: 18 additions & 0 deletions src/polyfills/FileReader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export class FileReader {
result: any = null;
error: any = null;
onload: ((event: Event) => void) | null = null;
onerror: ((event: Event) => void) | null = null;
onloadstart: ((event: Event) => void) | null = null;
onloadend: ((event: Event) => void) | null = null;
onprogress: ((event: Event) => void) | null = null;
readyState: number = 0;

abort(): void {}

readAsDataURL(file: File): void {
this.readyState = 2;
const readerEvent = new Event('load');
this.onloadend && this.onloadend(readerEvent);
}
}

0 comments on commit 375ffdd

Please sign in to comment.