Spaces:
Sleeping
Sleeping
Update upload.py
Browse files
upload.py
CHANGED
|
@@ -2,8 +2,24 @@ import os
|
|
| 2 |
import uuid
|
| 3 |
import requests
|
| 4 |
import oss2
|
|
|
|
| 5 |
from requests.cookies import RequestsCookieJar
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def upload_image_to_cdn(base_url, auth_token, image_path):
|
| 8 |
"""
|
| 9 |
Uploads an image to the CDN using STS credentials.
|
|
@@ -118,7 +134,7 @@ def upload_audio_to_cdn(base_url, auth_token, audio_path):
|
|
| 118 |
def _create_headers(self):
|
| 119 |
return {
|
| 120 |
'x-request-id': str(uuid.uuid4()),
|
| 121 |
-
'Authorization': f'Bearer
|
| 122 |
'Content-Type': 'application/json',
|
| 123 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36'
|
| 124 |
}
|
|
@@ -152,19 +168,28 @@ def upload_audio_to_cdn(base_url, auth_token, audio_path):
|
|
| 152 |
bucket = oss2.Bucket(auth, endpoint, sts_data['bucketname'])
|
| 153 |
|
| 154 |
with open(file_path, 'rb') as file:
|
| 155 |
-
result = bucket.put_object(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
return result.status == 200
|
| 158 |
|
| 159 |
def upload_file(self, file_path, filetype):
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
# Step 1: Get STS token
|
| 164 |
sts_data = self.get_sts_token(filename, filesize, filetype)
|
| 165 |
|
| 166 |
# Step 2: Upload to OSS
|
| 167 |
-
upload_success = self.upload_to_oss(sts_data,
|
| 168 |
|
| 169 |
if upload_success:
|
| 170 |
return sts_data['file_url']
|
|
|
|
| 2 |
import uuid
|
| 3 |
import requests
|
| 4 |
import oss2
|
| 5 |
+
from pydub import AudioSegment # For audio conversion
|
| 6 |
from requests.cookies import RequestsCookieJar
|
| 7 |
|
| 8 |
+
def convert_to_mp3(input_path, output_path):
|
| 9 |
+
"""
|
| 10 |
+
Converts an audio file to MP3 format using pydub.
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
input_path (str): Path to the input audio file.
|
| 14 |
+
output_path (str): Path where the converted MP3 file will be saved.
|
| 15 |
+
|
| 16 |
+
Returns:
|
| 17 |
+
None
|
| 18 |
+
"""
|
| 19 |
+
sound = AudioSegment.from_file(input_path)
|
| 20 |
+
sound.export(output_path, format="mp3")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
def upload_image_to_cdn(base_url, auth_token, image_path):
|
| 24 |
"""
|
| 25 |
Uploads an image to the CDN using STS credentials.
|
|
|
|
| 134 |
def _create_headers(self):
|
| 135 |
return {
|
| 136 |
'x-request-id': str(uuid.uuid4()),
|
| 137 |
+
'Authorization': f'Bearer {self.auth_token}',
|
| 138 |
'Content-Type': 'application/json',
|
| 139 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36'
|
| 140 |
}
|
|
|
|
| 168 |
bucket = oss2.Bucket(auth, endpoint, sts_data['bucketname'])
|
| 169 |
|
| 170 |
with open(file_path, 'rb') as file:
|
| 171 |
+
result = bucket.put_object(
|
| 172 |
+
sts_data['file_path'],
|
| 173 |
+
file,
|
| 174 |
+
headers={'Content-Type': 'audio/mpeg'} # Set correct MIME type
|
| 175 |
+
)
|
| 176 |
|
| 177 |
return result.status == 200
|
| 178 |
|
| 179 |
def upload_file(self, file_path, filetype):
|
| 180 |
+
# Convert the audio file to MP3 format
|
| 181 |
+
mp3_path = f"{os.path.splitext(file_path)[0]}.mp3"
|
| 182 |
+
convert_to_mp3(file_path, mp3_path)
|
| 183 |
+
|
| 184 |
+
# Use the converted MP3 file for upload
|
| 185 |
+
filename = os.path.basename(mp3_path)
|
| 186 |
+
filesize = os.path.getsize(mp3_path)
|
| 187 |
|
| 188 |
# Step 1: Get STS token
|
| 189 |
sts_data = self.get_sts_token(filename, filesize, filetype)
|
| 190 |
|
| 191 |
# Step 2: Upload to OSS
|
| 192 |
+
upload_success = self.upload_to_oss(sts_data, mp3_path)
|
| 193 |
|
| 194 |
if upload_success:
|
| 195 |
return sts_data['file_url']
|