flydrive icon indicating copy to clipboard operation
flydrive copied to clipboard

Support for make public

Open ojhaujjwal opened this issue 4 years ago • 8 comments

Is there a way we can support making individual files public?

Use-case: for public images.

ojhaujjwal avatar May 05 '20 05:05 ojhaujjwal

It's won't be applicable for all the drivers like local; but gcp and s3 support it.

ojhaujjwal avatar May 05 '20 05:05 ojhaujjwal

php's flysystem library provides a similar feature image

ojhaujjwal avatar May 05 '20 05:05 ojhaujjwal

Hey @ojhaujjwal! 👋

We are happily accepting PR!

However, I'd recommend to wait until we finish to swap the architecture. I'll let you know.

RomainLanz avatar May 19 '20 22:05 RomainLanz

Thanks @RomainLanz. I will be happy to contribute after it's done.

ojhaujjwal avatar May 20 '20 05:05 ojhaujjwal

Hello everybody!

Here is my solution to this problem.

In my project I have the structure(AdonisJS V5):

/app/Services/Spaces -- index.ts -- AmazonWebServicesS3Storage.ts

File: index.ts

import { StorageManager } from '@slynova/flydrive'

import AmazonWebServicesS3Storage from './AmazonWebServicesS3Storage'

import config from '../../../config/storage'

const storage = new StorageManager(config)

storage.registerDriver('s3', AmazonWebServicesS3Storage)

export default storage

File: AmazonWebServicesS3Storage.ts

import { NoSuchBucket, FileNotFound, PermissionMissing, UnknownException } from '@slynova/flydrive'

import { AmazonWebServicesS3Storage } from '@slynova/flydrive-s3'

function handleError(err, path, bucket) {
  switch (err.name) {
    case 'NoSuchBucket':
      return new NoSuchBucket(err, bucket)
    case 'NoSuchKey':
      return new FileNotFound(err, path)
    case 'AllAccessDisabled':
      return new PermissionMissing(err, path)
    default:
      return new UnknownException(err, err.name, path)
  }
}

export default class MyAmazonWebServicesS3Storage extends AmazonWebServicesS3Storage {
  constructor(config) {
    super(config)
  }

  async putPublic(location: string, content: any) {
    const params = { Key: location, Body: content, Bucket: this.$bucket, ACL: 'public-read' }
    try {
      const result = await this.$driver.upload(params).promise()
      return { raw: result }
    } catch (e) {
      throw handleError(e, location, this.$bucket)
    }
  }
}

In a file in a distant world, the method is consumed like this:

import Route from '@ioc:Adonis/Core/Route'
import Spaces from 'App/Services/Spaces'

Route.post('spaces', async ({ request }) => {
  const file = request.file('name'),
    name = file.name

  return Spaces.disk('spaces').putPublic(name, file)
})

I extended the original AmazonWebServicesS3Storage class by adding the putPublic method with the 'public-read' ACL rule.

It is! MTF solution but functional!

🙃

DealerUp avatar Dec 18 '20 03:12 DealerUp

Hello everybody!

Here is my solution to this problem.

In my project I have the structure(AdonisJS V5):

/app/Services/Spaces -- index.ts -- AmazonWebServicesS3Storage.ts

File: index.ts

import { StorageManager } from '@slynova/flydrive'

import AmazonWebServicesS3Storage from './AmazonWebServicesS3Storage'

import config from '../../../config/storage'

const storage = new StorageManager(config)

storage.registerDriver('s3', AmazonWebServicesS3Storage)

export default storage

How is the configuration of your StorageManager?

TrickSantos avatar Jan 12 '21 13:01 TrickSantos

Hello everybody! Here is my solution to this problem. In my project I have the structure(AdonisJS V5): /app/Services/Spaces -- index.ts -- AmazonWebServicesS3Storage.ts File: index.ts

import { StorageManager } from '@slynova/flydrive'

import AmazonWebServicesS3Storage from './AmazonWebServicesS3Storage'

import config from '../../../config/storage'

const storage = new StorageManager(config)

storage.registerDriver('s3', AmazonWebServicesS3Storage)

export default storage

How is the configuration of your StorageManager?

import Env from '@ioc:Adonis/Core/Env'
import path from 'path'

export const ROOT_PATH = path.resolve(__dirname, '../')
export const STORAGE_PATH = path.resolve(ROOT_PATH, 'resources/storage')
export const PUBLIC_PATH = path.resolve(ROOT_PATH, 'public')

export default {
  default: 'local',

  disks: {
    local: {
      driver: 'local',
      config: {
        root: STORAGE_PATH,
      },
    },

    public: {
      driver: 'local',
      config: {
        root: PUBLIC_PATH,
      },
    },

    spaces: {
      driver: 's3',
      config: {
        key: Env.get('DO_SPACES_KEY'),
        secret: Env.get('DO_SPACES_SECRET'),
        endpoint: Env.get('DO_SPACES_ENDPOINT'),
        bucket: Env.get('DO_SPACES_SPACE'),
        region: Env.get('DO_SPACES_REGION'),
      },
    },
  },
}

DealerUp avatar Jan 20 '21 12:01 DealerUp

Sent a PR #166

njsubedi avatar Jul 20 '21 12:07 njsubedi