Spaces:
Sleeping
Sleeping
feat: add Cloudinary support as an alternative image storage mode
Browse filesFiles changed (3)
72 unmodified lines737475767778793 unmodified lines83848586878872 unmodified linesreturn f"redis://{self.REDIS_HOST}:{self.REDIS_PORT}/0"# Storage ConfigurationsSTORAGE_MODE: str = "local" # "local" hoặc "r2"UPLOAD_DIR: str = "uploads"SERVER_HOST_URL: str = "http://localhost:8000"3 unmodified linesR2_SECRET_ACCESS_KEY: str = "change_me_r2_secret_key"R2_ENDPOINT_URL: str = "https://change-me-account-id.r2.cloudflarestorage.com"# Qdrant Vector DBQDRANT_HOST: str = "localhost"72 unmodified lines737475767778793 unmodified lines838485868788899091929372 unmodified linesreturn f"redis://{self.REDIS_HOST}:{self.REDIS_PORT}/0"# Storage ConfigurationsSTORAGE_MODE: str = "local" # "local", "r2", hoặc "cloudinary"UPLOAD_DIR: str = "uploads"SERVER_HOST_URL: str = "http://localhost:8000"3 unmodified linesR2_SECRET_ACCESS_KEY: str = "change_me_r2_secret_key"R2_ENDPOINT_URL: str = "https://change-me-account-id.r2.cloudflarestorage.com"# Cloudinary ConfigsCLOUDINARY_CLOUD_NAME: Optional[str] = NoneCLOUDINARY_API_KEY: Optional[str] = NoneCLOUDINARY_API_SECRET: Optional[str] = None# Qdrant Vector DBQDRANT_HOST: str = "localhost"
14 unmodified lines15161718192021 unmodified lines42434445464737 unmodified lines85868788899014 unmodified linesaws_secret_access_key=settings.R2_SECRET_ACCESS_KEY,endpoint_url=settings.R2_ENDPOINT_URL)else:self.s3_client = None21 unmodified linesexcept Exception as e:logging.error(f"Failed to upload local file: {e}")return Noneelse:# Cloudflare R2 Uploadtry:37 unmodified linesexcept Exception as e:logging.error(f"Failed to delete local file: {e}")return Falseelse:try:if not self.s3_client:14 unmodified lines1516171819202122232425262728293021 unmodified lines5253545556575859606162636465666768697071727374757637 unmodified lines11411511611711811912012112212312412512612712812913013113213313413513613713813914014114 unmodified linesaws_secret_access_key=settings.R2_SECRET_ACCESS_KEY,endpoint_url=settings.R2_ENDPOINT_URL)elif settings.STORAGE_MODE == "cloudinary":self.s3_client = Noneimport cloudinaryimport cloudinary.uploadercloudinary.config(cloud_name=settings.CLOUDINARY_CLOUD_NAME,api_key=settings.CLOUDINARY_API_KEY,api_secret=settings.CLOUDINARY_API_SECRET,secure=True)else:self.s3_client = None21 unmodified linesexcept Exception as e:logging.error(f"Failed to upload local file: {e}")return Noneelif settings.STORAGE_MODE == "cloudinary":try:import cloudinary.uploader# Parse folder path and filename from sanitized pathpath_parts = sanitized_object_name.split("/")folder = "wardrobe/" + "/".join(path_parts[:-1]) if len(path_parts) > 1 else "wardrobe"filename = os.path.splitext(path_parts[-1])[0]result = cloudinary.uploader.upload(file_content,folder=folder,public_id=filename,overwrite=True,resource_type="auto")return result.get("secure_url")except Exception as e:logging.error(f"Failed to upload file to Cloudinary: {e}")return Noneelse:# Cloudflare R2 Uploadtry:37 unmodified linesexcept Exception as e:logging.error(f"Failed to delete local file: {e}")return Falseelif settings.STORAGE_MODE == "cloudinary":try:import cloudinary.uploaderimport re# Parse public_id from Cloudinary URL:# https://res.cloudinary.com/<cloud_name>/image/upload/v<version>/<folder>/<public_id>.<ext>if "res.cloudinary.com" in object_name_or_url:match = re.search(r"/upload/(?:v\d+/)?(.+)$", object_name_or_url)if match:public_id_with_ext = match.group(1)public_id = os.path.splitext(public_id_with_ext)[0]else:public_id = object_name_or_urlelse:public_id = object_name_or_urlresult = cloudinary.uploader.destroy(public_id)return result.get("result") == "ok"except Exception as e:logging.error(f"Failed to delete file from Cloudinary: {e}")return Falseelse:try:if not self.s3_client:
41 unmodified lines4243444541 unmodified lines"huggingface-hub>=0.23.0","matplotlib>=3.9.1","tqdm>=4.66.4",]41 unmodified lines424344454641 unmodified lines"huggingface-hub>=0.23.0","matplotlib>=3.9.1","tqdm>=4.66.4","cloudinary>=1.41.0",]
app/core/config.py+6-1
app/services/storage.py+51
pyproject.toml+1