Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
27 additions
and
10 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
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,15 +1,30 @@ | ||
import mimetypes | ||
from os.path import join, basename, dirname | ||
from os.path import basename | ||
from .config import Config | ||
from werkzeug.utils import secure_filename | ||
from slugify import slugify | ||
mimetypes.init() | ||
import boto3 | ||
|
||
storage_endpoint = "https://eu-central-1.linodeobjects.com" | ||
bucket_name = "cc0" | ||
bucket_url = f"{storage_endpoint}/{bucket_name}" | ||
storage = boto3.resource( | ||
's3', | ||
endpoint_url=storage_endpoint, | ||
region_name='eu-central-1', | ||
aws_access_key_id=Config.OBJECT_KEY_ID, | ||
aws_secret_access_key=Config.OBJECT_ACCESS_KEY | ||
) | ||
bucket = storage.Bucket(bucket_name) | ||
|
||
def make_public(name): | ||
storage.Object(bucket_name, name).Acl().put(ACL='public-read') | ||
|
||
def get_url_for(file, username, collection_slug, piece_slug): | ||
name = f'{username}-{collection_slug}-{piece_slug}-{secure_filename(basename(file.filename))}' | ||
path = join(dirname(__file__), "static", name) | ||
file.save(path) | ||
|
||
return f"/static/{name}" | ||
bucket.upload_fileobj(file.stream, name) | ||
make_public(name) | ||
return f"{bucket_url}/{name}" | ||
|
||
def get_type_for(file): | ||
return mimetypes.guess_type(file.filename)[0] |
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