fixing upload...

This commit is contained in:
tonyrewin 2023-01-10 10:48:59 +03:00
parent c0fbfc29ab
commit 97d30f155d
6 changed files with 52 additions and 32 deletions

View File

@ -1,3 +1,8 @@
[0.7.1]
[+] reactions CRUL
[+] upload with storj
[0.7.0] [0.7.0]
[+] inbox: context provider, chats [+] inbox: context provider, chats
[+] comments: show [+] comments: show

View File

@ -1,4 +1,4 @@
from flask import Flask, request from flask import Flask, request, jsonify
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
import boto3 import boto3
from botocore.exceptions import ClientError, WaiterError from botocore.exceptions import ClientError, WaiterError
@ -17,7 +17,7 @@ storj_client = boto3.client('s3',
def upload_storj(filecontent, filename, bucket_name): def upload_storj(filecontent, filename, bucket_name):
head = None head = None
bucket_obj = None
try: try:
bucket = storj_resource.Bucket(bucket_name) bucket = storj_resource.Bucket(bucket_name)
except ClientError: except ClientError:
@ -33,22 +33,22 @@ def upload_storj(filecontent, filename, bucket_name):
etag = head['ETag'].strip('"') etag = head['ETag'].strip('"')
try: try:
s3_obj = bucket.Object(filename) bucket_obj = bucket.Object(filename)
except (ClientError, AttributeError): except (ClientError, AttributeError):
s3_obj = None bucket_obj = None
try: try:
# Use the upload_fileobj method to safely upload the file # Use the upload_fileobj method to safely upload the file
storj_client.upload_fileobj( storj_client.upload_fileobj(
Fileobj=filecontent, Fileobj=filecontent,
Bucket='discours.io', Bucket=bucket_name,
Key=filename Key=filename
) )
except (ClientError, AttributeError): except (ClientError, AttributeError):
pass pass
else: else:
try: try:
s3_obj.wait_until_exists(IfNoneMatch=etag) bucket_obj.wait_until_exists(IfNoneMatch=etag)
except WaiterError: except WaiterError:
pass pass
else: else:
@ -56,22 +56,21 @@ def upload_storj(filecontent, filename, bucket_name):
return head return head
@app.route('/upload', methods=['post']) @app.route('/api/upload', methods=['post'])
def upload(): def upload():
if request.method == 'POST': print('upload serverless route is fine')
img = request.files['file'] print(request.path)
if img: print(request.form)
# Perform the file upload print(request.files)
filename = secure_filename(img.filename) img = request.files['file']
# Save the file to a temporary location if img:
with tempfile.TemporaryDirectory() as temp_dir: # Perform the file upload
temp_path = os.path.join(temp_dir, filename) filename = secure_filename(img.filename)
img.save(temp_path) # Save the file to a temporary location
# Open the file in binary mode with tempfile.TemporaryDirectory() as temp_dir:
with open(temp_path, 'rb') as filecontent: temp_path = os.path.join(temp_dir, filename)
return upload_storj(filecontent, filename, 'discours.io') img.save(temp_path)
# Open the file in binary mode
with open(temp_path, 'rb') as filecontent:
return jsonify(upload_storj(filecontent, filename, 'discoursio'))
return return
if __name__ == "__main__":
app.run()

View File

@ -37,6 +37,7 @@ export const ProfileSettingsPage = (props: PageProps) => {
formData.append('type', file.type) formData.append('type', file.type)
formData.append('name', image.source.split('/').pop()) formData.append('name', image.source.split('/').pop())
formData.append('ext', image.name.split('.').pop()) formData.append('ext', image.name.split('.').pop())
formData.append('data', image.file)
const resp = await fetch('/api/upload', { const resp = await fetch('/api/upload', {
method: 'POST', method: 'POST',
body: formData body: formData

View File

@ -4,18 +4,19 @@
.popup { .popup {
background: #fff; background: #fff;
top: calc(100% + 8px);
opacity: 1;
color: #000; color: #000;
position: absolute;
z-index: 100;
min-width: 144px; min-width: 144px;
opacity: 1;
position: absolute;
top: calc(100% + 8px);
z-index: 100;
ul { ul {
margin-bottom: 0; margin-bottom: 0;
li { li {
position: relative; position: relative;
&:last-child { &:last-child {
margin-bottom: 0; margin-bottom: 0;
} }
@ -24,11 +25,12 @@
&.bordered { &.bordered {
@include font-size(1.6rem); @include font-size(1.6rem);
border: 2px solid #000; border: 2px solid #000;
padding: 2.4rem; padding: 2.4rem;
ul li { ul li {
margin-bottom: 1.6rem; margin-bottom: 1.6rem;
&:last-child { &:last-child {
margin-bottom: 0; margin-bottom: 0;
} }
@ -37,11 +39,12 @@
&.tiny { &.tiny {
@include font-size(1.4rem); @include font-size(1.4rem);
box-shadow: 0 4px 60px rgba(0, 0, 0, 0.1); box-shadow: 0 4px 60px rgba(0, 0, 0, 0.1);
padding: 1rem; padding: 1rem;
ul li { ul li {
margin-bottom: 1rem; margin-bottom: 1rem;
&:last-child { &:last-child {
margin-bottom: 0; margin-bottom: 0;
} }

View File

@ -4,8 +4,20 @@ import Prerendered from '../main.astro'
import { apiClient } from '../utils/apiClient' import { apiClient } from '../utils/apiClient'
import { initRouter } from '../stores/router' import { initRouter } from '../stores/router'
const excludes = [
'authors',
'connect',
'create',
'inbox',
'search',
'topics',
'welcome',
'confirm',
'feed'
]
const slug = Astro.params.slug?.toString() const slug = Astro.params.slug?.toString()
if (slug.endsWith('.map')) { if (slug.endsWith('.map') || slug in excludes) {
return Astro.redirect('/404') return Astro.redirect('/404')
} }

View File

@ -12,8 +12,8 @@ import type {
MutationCreateMessageArgs, MutationCreateMessageArgs,
Chat, Chat,
QueryLoadRecipientsArgs, QueryLoadRecipientsArgs,
ReactionBy, ProfileInput,
ProfileInput ReactionBy
} from '../graphql/types.gen' } from '../graphql/types.gen'
import { publicGraphQLClient } from '../graphql/publicGraphQLClient' import { publicGraphQLClient } from '../graphql/publicGraphQLClient'
import { getToken, privateGraphQLClient } from '../graphql/privateGraphQLClient' import { getToken, privateGraphQLClient } from '../graphql/privateGraphQLClient'