Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DA-253] storage api methods with asset service and local filesystem backends #199

Draft
wants to merge 8 commits into
base: anima_stable
Choose a base branch
from

Conversation

aletts
Copy link

@aletts aletts commented Mar 26, 2023

This adds an abstract StorageBackend class with AssetServiceBackend and LocalFileBackend. One backend can be chosen as the primary for reads, while any number of backends can be chosen for writes.

This initial API is modeled to support the existing asset service functionality for projects and to prioritize similarity between files saved locally and the asset service (to serve as a mirror). As such, the file organization is flat and uses uuid's. Future plans may involve a more convenient structure - and may leverage a suitable existing library.

@github-advanced-security
Copy link

You have successfully added a new SonarCloud configuration ``. As part of the setup process, we have scanned this repository and found no existing alerts. In the future, you will see all code scanning alerts on the repository Security tab.

Copy link
Member

@pharmapsychotic pharmapsychotic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be able to simplify by having the backends with just get/put of bytes and short methods that call those underlying ones as convenience for image/video/etc.

proj = {"id": proj_id,
"title": title,
"file": {"id": proj_file_id}}
output_path = os.path.join(LocalFileBackend._projects_root, proj_id, proj_file_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Projects are currently stored in subfolders based on the project name which would be nice to preserve so people can browse their Google/local drive and find things easy
https://github.com/Stability-AI/stability-sdk/blob/anima_stable/src/stability_sdk/animation_ui.py#L424

src/stability_sdk/api.py Outdated Show resolved Hide resolved
src/stability_sdk/api.py Outdated Show resolved Hide resolved
Comment on lines +635 to +668
@staticmethod
def add_asset_metadata(project_id: str, asset_id: str, mime_type: str, filename: str, project_key: str = None) -> None:
# metadata_index = self.load_metadata_index() # I assume metadata is updated by each operation
if project_id not in Project._metadata_index:
Project._metadata_index[project_id] = {}
Project._metadata_index[project_id][asset_id] = {
"mime_type": mime_type
}
if filename is not None:
Project._metadata_index[project_id][asset_id]["filename"] = filename
if project_key is not None:
Project._metadata_index[project_id][project_key] = asset_id
Project.save_metadata_index()

@staticmethod
def delete_project_metadata(project_id: str) -> None:
Project._metadata_index.pop(project_id, None)

@classmethod
def save_metadata_index(cls, metadata_index: dict = None) -> None:
if metadata_index is None:
metadata_index = cls._metadata_index
index_file = f"metadata_index.json"
with open(index_file, "w") as f:
json.dump(metadata_index, f)

@classmethod
def load_metadata_index(cls) -> dict:
index_file = "metadata_index.json"
if os.path.exists(index_file):
with open(index_file, "r") as f:
metadata_index = json.load(f)
return metadata_index
return {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the metadata index necessary? Could it be implicit based on what's in the folders?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking on this was basically that for future purposes, I have an interest in representing this information in application memory. This metadata JSON saved to disk is an easy dump for bootstrapping reasons since we don't have a UI to look at what the in-memory representation.
I agree that it should currently be considered a cache at best, since someone could potentially create/delete/etc. projects+assets by other means (, and the metadata isn't saying much else interesting aside from what can be found in projects+assets).. and so the sync/refresh strategy needs some work. I've planned to at least add a script to download all assets and rebuild metadata, but haven't written it yet.
For persistent storage, I'd like to see everything kept in the project+assets rather than solely metadata.

@sonarcloud
Copy link

sonarcloud bot commented Mar 29, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 28 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants