-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #212 from dnlbauer/stream_crate
Stream RO-Crate Zip
- Loading branch information
Showing
45 changed files
with
448 additions
and
80 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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Copyright 2019-2024 The University of Manchester, UK | ||
# Copyright 2020-2024 Vlaams Instituut voor Biotechnologie (VIB), BE | ||
# Copyright 2020-2024 Barcelona Supercomputing Center (BSC), ES | ||
# Copyright 2020-2024 Center for Advanced Studies, Research and Development in Sardinia (CRS4), IT | ||
# Copyright 2022-2024 École Polytechnique Fédérale de Lausanne, CH | ||
# Copyright 2024 Data Centre, SciLifeLab, SE | ||
# Copyright 2024 National Institute of Informatics (NII), JP | ||
# Copyright 2025 Senckenberg Society for Nature Research (SGN), DE | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
Streaming RO-Crates from a web server | ||
This example demonstrates how to create an RO-Crate on-the-fly | ||
and stream the result to the client. | ||
By using `stream_zip`, the RO-Crate is not written to disk and remote | ||
data is only fetched on the fly. | ||
To run: `fastapi dev main.py`, then visit http://localhost:8000/crate | ||
""" | ||
|
||
from fastapi import FastAPI | ||
from fastapi.responses import StreamingResponse | ||
from rocrate.rocrate import ROCrate | ||
from io import StringIO | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.get("/crate") | ||
async def get(): | ||
crate = ROCrate() | ||
|
||
# Add a remote file | ||
crate.add_file( | ||
"https://raw.githubusercontent.com/ResearchObject/ro-crate-py/refs/heads/master/test/test-data/sample_file.txt", | ||
fetch_remote=True | ||
) | ||
|
||
# Add a file containing a string to the crate | ||
crate.add_file( | ||
source=StringIO("Hello, World!"), | ||
dest_path="test-data/hello.txt" | ||
) | ||
|
||
# Stream crate to client as a zip file | ||
return StreamingResponse( | ||
crate.stream_zip(), | ||
media_type="application/rocrate+zip", | ||
headers={ | ||
"Content-Disposition": "attachment; filename=crate.zip", | ||
} | ||
) |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
../../ | ||
fastapi | ||
fastapi-cli |
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
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Copyright 2019-2024 The University of Manchester, UK | ||
# Copyright 2020-2024 Vlaams Instituut voor Biotechnologie (VIB), BE | ||
# Copyright 2020-2024 Barcelona Supercomputing Center (BSC), ES | ||
# Copyright 2020-2024 Center for Advanced Studies, Research and Development in Sardinia (CRS4), IT | ||
# Copyright 2022-2024 École Polytechnique Fédérale de Lausanne, CH | ||
# Copyright 2024 Data Centre, SciLifeLab, SE | ||
# Copyright 2024 National Institute of Informatics (NII), JP | ||
# Copyright 2025 Senckenberg Society for Nature Research (SGN), DE | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from io import RawIOBase | ||
|
||
|
||
class MemoryBuffer(RawIOBase): | ||
""" | ||
A buffer class that supports reading and writing binary data. | ||
The buffer automatically resets upon reading to make sure all data is read only once. | ||
""" | ||
|
||
def __init__(self): | ||
self._buffer = b'' | ||
|
||
def write(self, data): | ||
if self.closed: | ||
raise ValueError('write to closed file') | ||
self._buffer += data | ||
return len(data) | ||
|
||
def read(self, size=-1): | ||
if self.closed: | ||
raise ValueError('read from closed file') | ||
if size < 0: | ||
data = self._buffer | ||
self._buffer = b'' | ||
else: | ||
data = self._buffer[:size] | ||
self._buffer = self._buffer[size:] | ||
return data | ||
|
||
def __len__(self): | ||
return len(self._buffer) |
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
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
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
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
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
Oops, something went wrong.