Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failing file upload to Supabase #22293

Merged
merged 8 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/green-sloths-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---

Check warning on line 1 in .changeset/green-sloths-sparkle.md

View workflow job for this annotation

GitHub Actions / Lint

File ignored by default.
'@directus/storage-driver-supabase': patch
---

Fixed file upload error for Supabase Storage servers hosted behind a Cloudflare Cache
101 changes: 50 additions & 51 deletions packages/storage-driver-supabase/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,70 +333,69 @@ describe('#read', () => {
});
});

describe('#head', () => {
test('Returns object headers', async () => {
vi.mocked(fetch).mockReturnValue({
status: 200,
headers: { 'content-length': sample.file.size, 'last-modified': sample.file.modified },
} as unknown as Promise<Response>);

const result = await driver.head(sample.path.input);

expect(result).toStrictEqual({ 'content-length': sample.file.size, 'last-modified': sample.file.modified });
});

test('Ensures input is passed to getAuthenticatedUrl', async () => {
vi.mocked(fetch).mockReturnValue({
status: 200,
headers: { 'content-length': sample.file.size, 'last-modified': sample.file.modified },
} as unknown as Promise<Response>);
describe('#stat', () => {
test('Returns the size/modified from metadata', async () => {
driver['bucket'] = {
list: vi.fn().mockReturnValue({
data: [{ metadata: { contentLength: sample.file.size, lastModified: sample.file.modified } }],
error: null,
}),
} as any;

driver['getAuthenticatedUrl'] = vi.fn();
const stat = await driver.stat(sample.path.input);

await driver.head(sample.path.input);
expect(stat).toEqual({
size: sample.file.size,
modified: sample.file.modified,
});

expect(driver['getAuthenticatedUrl']).toHaveBeenCalledWith(sample.path.input);
expect(driver['bucket'].list).toHaveBeenCalledWith('', {
limit: 1,
search: sample.path.input,
});
});

test('Throws an error when a status >= 400 is sent', async () => {
vi.mocked(fetch).mockReturnValue({
status: 400,
headers: { 'content-length': sample.file.size, 'last-modified': sample.file.modified },
} as unknown as Promise<Response>);
test('Uses the configured root directory', async () => {
driver['config'].root = 'root';

expect(driver.head(sample.path.input)).rejects.toThrowError(new Error(`File not found`));
});
});

describe('#stat', () => {
test('Returns size/modified from returned send data', async () => {
vi.mocked(fetch).mockReturnValue({
status: 200,
headers: {
get(key: any) {
if (key === 'content-length') {
return sample.file.size;
} else if (key === 'last-modified') {
return sample.file.modified;
}

return null;
},
},
} as unknown as Promise<Response>);
driver['bucket'] = {
list: vi.fn().mockReturnValue({
data: [{ metadata: { contentLength: sample.file.size, lastModified: sample.file.modified } }],
error: null,
}),
} as any;

const result = await driver.stat(sample.path.input);
const stat = await driver.stat(sample.path.input);

expect(result).toStrictEqual({
expect(stat).toEqual({
size: sample.file.size,
modified: sample.file.modified,
});

expect(driver['bucket'].list).toHaveBeenCalledWith('root', {
limit: 1,
search: sample.path.input,
});
});

test('Throws an error no file is returned by list', async () => {
driver['bucket'] = {
list: vi.fn().mockReturnValue({
data: [],
error: null,
}),
} as any;

expect(driver.stat(sample.path.input)).rejects.toThrowError(new Error(`File not found`));
});

test('Throws an error when a status >= 400 is sent', async () => {
vi.mocked(fetch).mockReturnValue({
status: 400,
} as unknown as Promise<Response>);
test('Throws an error if storage error is returned', async () => {
driver['bucket'] = {
list: vi.fn().mockReturnValue({
data: null,
error: true,
}),
} as any;

expect(driver.stat(sample.path.input)).rejects.toThrowError(new Error(`File not found`));
});
Expand Down
22 changes: 7 additions & 15 deletions packages/storage-driver-supabase/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,19 @@ export class DriverSupabase implements Driver {
return Readable.fromWeb(response.body);
}

async head(filepath: string) {
const response = await fetch(this.getAuthenticatedUrl(filepath), {
method: 'HEAD',
headers: {
Authorization: `Bearer ${this.config.serviceRole}`,
},
async stat(filepath: string) {
const { data, error } = await this.bucket.list(this.config.root, {
search: filepath,
limit: 1,
});

if (response.status >= 400) {
if (error || data.length === 0) {
throw new Error('File not found');
}

return response.headers;
}

async stat(filepath: string) {
const headers = await this.head(filepath);

return {
size: parseInt(headers.get('content-length') || ''),
modified: new Date(headers.get('last-modified') || ''),
size: data[0]?.metadata['contentLength'] ?? 0,
modified: new Date(data[0]?.metadata['lastModified'] || null),
};
}

Expand Down