-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #161 from autonomys/public-download-links
feat: enable creation of public downloadable links
- Loading branch information
Showing
22 changed files
with
460 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
type BaseTokenPayload = { | ||
id: string | ||
isRefreshToken: boolean | ||
oauthProvider: string | ||
oauthUserId: string | ||
} | ||
id: string; | ||
isRefreshToken: boolean; | ||
oauthProvider: string; | ||
oauthUserId: string; | ||
}; | ||
|
||
export type CustomAccessTokenPayload = BaseTokenPayload & { | ||
isRefreshToken: false | ||
refreshTokenId: string | ||
'https://hasura.io/jwt/claims': { | ||
'x-hasura-default-role': string | ||
'x-hasura-allowed-roles': string[] | ||
'x-hasura-oauth-provider': string | ||
'x-hasura-oauth-user-id': string | ||
'x-hasura-organization-id': string | ||
} | ||
} | ||
isRefreshToken: false; | ||
refreshTokenId: string; | ||
"https://hasura.io/jwt/claims": { | ||
"x-hasura-default-role": string; | ||
"x-hasura-allowed-roles": string[]; | ||
"x-hasura-oauth-provider": string; | ||
"x-hasura-oauth-user-id": string; | ||
"x-hasura-organization-id": string; | ||
"x-hasura-public-id": string; | ||
}; | ||
}; | ||
|
||
export type CustomRefreshTokenPayload = BaseTokenPayload & { | ||
isRefreshToken: true | ||
} | ||
isRefreshToken: true; | ||
}; | ||
|
||
export type CustomTokenPayload = | ||
| CustomAccessTokenPayload | ||
| CustomRefreshTokenPayload | ||
| CustomRefreshTokenPayload; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { InteractionType } from '../../../src/models/objects/interactions' | ||
import { UserWithOrganization } from '../../../src/models/users/user' | ||
import { PublishedObject } from '../../../src/repositories/objects/publishedObjects' | ||
import { AuthManager } from '../../../src/services/auth' | ||
import { ObjectUseCases, SubscriptionsUseCases } from '../../../src/useCases' | ||
import { asyncIterableToPromiseOfArray } from '../../../src/utils/async' | ||
import { dbMigration } from '../../utils/dbMigrate' | ||
import { createMockUser } from '../../utils/mocks' | ||
import { uploadFile } from '../../utils/uploads' | ||
import { jest } from '@jest/globals' | ||
|
||
describe('Public URL', () => { | ||
let user: UserWithOrganization | ||
let fileCid: string | ||
let publishedObject: PublishedObject | ||
const content = 'test' | ||
|
||
beforeAll(async () => { | ||
await dbMigration.up() | ||
user = createMockUser() | ||
fileCid = await uploadFile(user, 'test.txt', content, 'text/plain') | ||
}) | ||
|
||
afterAll(async () => { | ||
await dbMigration.down() | ||
}) | ||
|
||
it('should be able to publish and retrieve', async () => { | ||
publishedObject = await ObjectUseCases.publishObject(user, fileCid) | ||
|
||
expect(publishedObject).toMatchObject({ | ||
publicId: user.publicId, | ||
cid: fileCid, | ||
id: expect.any(String), | ||
}) | ||
}) | ||
|
||
it('should be downloadable by public id and credits should be deducted', async () => { | ||
jest.spyOn(AuthManager, 'getUserFromPublicId').mockResolvedValue(user) | ||
|
||
const { metadata, startDownload } = | ||
await ObjectUseCases.downloadPublishedObject(publishedObject.id) | ||
|
||
expect(metadata).toMatchObject({ | ||
type: 'file', | ||
dataCid: publishedObject.cid, | ||
}) | ||
|
||
const pendingCredits = | ||
await SubscriptionsUseCases.getPendingCreditsByUserAndType( | ||
user, | ||
InteractionType.Download, | ||
) | ||
|
||
const downloadedContent = Buffer.concat( | ||
await asyncIterableToPromiseOfArray(await startDownload()), | ||
) | ||
expect(downloadedContent).toEqual(Buffer.from(content)) | ||
|
||
const updatedPendingCredits = | ||
await SubscriptionsUseCases.getPendingCreditsByUserAndType( | ||
user, | ||
InteractionType.Download, | ||
) | ||
|
||
expect(updatedPendingCredits).toBe( | ||
pendingCredits - downloadedContent.length, | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict' | ||
|
||
var dbm | ||
var type | ||
var seed | ||
var fs = require('fs') | ||
var path = require('path') | ||
var Promise | ||
|
||
/** | ||
* We receive the dbmigrate dependency from dbmigrate initially. | ||
* This enables us to not have to rely on NODE_PATH. | ||
*/ | ||
exports.setup = function (options, seedLink) { | ||
dbm = options.dbmigrate | ||
type = dbm.dataType | ||
seed = seedLink | ||
Promise = options.Promise | ||
} | ||
|
||
exports.up = function (db) { | ||
var filePath = path.join( | ||
__dirname, | ||
'sqls', | ||
'20250130141940-add-public-urls-up.sql', | ||
) | ||
return new Promise(function (resolve, reject) { | ||
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) { | ||
if (err) return reject(err) | ||
|
||
resolve(data) | ||
}) | ||
}).then(function (data) { | ||
return db.runSql(data) | ||
}) | ||
} | ||
|
||
exports.down = function (db) { | ||
var filePath = path.join( | ||
__dirname, | ||
'sqls', | ||
'20250130141940-add-public-urls-down.sql', | ||
) | ||
return new Promise(function (resolve, reject) { | ||
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) { | ||
if (err) return reject(err) | ||
|
||
resolve(data) | ||
}) | ||
}).then(function (data) { | ||
return db.runSql(data) | ||
}) | ||
} | ||
|
||
exports._meta = { | ||
version: 1, | ||
} |
3 changes: 3 additions & 0 deletions
3
backend/migrations/sqls/20250130141940-add-public-urls-down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
DROP INDEX IF EXISTS idx_published_objects_public_id; | ||
DROP INDEX IF EXISTS idx_published_objects_cid; | ||
DROP TABLE IF EXISTS published_objects; |
10 changes: 10 additions & 0 deletions
10
backend/migrations/sqls/20250130141940-add-public-urls-up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TABLE published_objects ( | ||
id text PRIMARY KEY, | ||
public_id text NOT NULL, | ||
cid text NOT NULL, | ||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
); | ||
|
||
CREATE INDEX idx_published_objects_public_id ON published_objects(public_id); | ||
CREATE INDEX idx_published_objects_cid ON published_objects(cid); |
Oops, something went wrong.