-
Notifications
You must be signed in to change notification settings - Fork 1
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 #165 from kartAI/provekode
ZIP fastAPI shell
- Loading branch information
Showing
44 changed files
with
178 additions
and
14 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
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
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 |
---|---|---|
@@ -1 +1 @@ | ||
{"Config": {"data_parameters": ["60", "40", "5"], "layers": ["Bygning", "Veg", "Bru"], "colors": ["#000000", "#ffff00", "#00ff00"]}} | ||
{"Config": {"data_parameters": ["90", "10", "10"], "layers": ["Bygning", "Veg", "Bru"], "colors": ["#000000", "#ffff00", "#00ff00"]}} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
{"Coordinates": [[588751.5420105711, 6642899.275265058], [588749.8344259012, 6642970.498298281], [589238.8910459754, 6642982.255880976], [589240.6080427505, 6642911.032997987], [588751.5420105711, 6642899.275265058]]} | ||
{"Coordinates": [[586077.6407936335, 6646104.917552568], [586025.9038943398, 6648327.088944961], [588710.10992247, 6648390.564976909], [588763.4615420719, 6646168.418973851], [586077.6407936335, 6646104.917552568]]} |
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import smtplib | ||
import ssl | ||
import zipfile | ||
from zipfile import ZipFile | ||
import base64 | ||
import sendgrid | ||
import asyncio | ||
|
@@ -23,6 +24,7 @@ | |
from fastapi.middleware.cors import CORSMiddleware | ||
from pydantic import BaseModel | ||
from deleteFolder import delete_all_folders | ||
from WMS import util | ||
|
||
# Class for the FastAPI. Will contain all our methods for updating values and starting scripts | ||
|
||
|
@@ -35,8 +37,6 @@ class ConfigInput(BaseModel): | |
layers: list | ||
colors: list | ||
|
||
|
||
|
||
# Import and create instance of the FastAPI framework | ||
app = FastAPI() | ||
|
||
|
@@ -308,3 +308,96 @@ async def update_wms_config_file(configInput: ConfigInput): | |
except Exception as e: | ||
raise HTTPException(status_code=500, detail=f"Failed to write config to json file: {str(e)}") | ||
return {"Message": "Config was updated successfully"} | ||
|
||
|
||
@app.post("/generatePhotos") | ||
async def generatePhotos(): | ||
|
||
#Read config from the file | ||
file = open(CONFIG_FILE) | ||
data = json.load(file) | ||
config = data["Config"]; | ||
|
||
|
||
#Her må de forskjellige WMSene plugges inn | ||
#generate_wms_picture(coordinates[0], coordinates[1], coordinates[2], coordinates[3]) | ||
#generate_wms_photo(coordinates, config) | ||
|
||
#Også må de riktige urlene plugges inn som image_path | ||
|
||
util.split_image("WMS/rawphotos/fasit.png", "WMS/tiles/fasit", 100) | ||
tiles = util.split_image("WMS/rawphotos/orto.png", "WMS/tiles/orto", 100) | ||
util.split_files("WMS/tiles", "email", tiles, config["data_parameters"][0], config["data_parameters"][1]) | ||
|
||
# Her begynner fil zipping og epost sending for WMS/Fasit | ||
|
||
# Finner path til .env filen som ligger i ngisopenapi mappen | ||
current_script_directory = os.path.dirname(os.path.abspath(__file__)) | ||
project_root = os.path.abspath(os.path.join(current_script_directory, '..', 'ngisopenapi')) | ||
env_file_path = os.path.join(project_root, '.env') | ||
|
||
# Laster .env fra riktig path | ||
load_dotenv(env_file_path) | ||
|
||
|
||
def send_email_with_attachment(to_emails, subject, content, attachment_path): | ||
"""Define email sending through SendGrid""" | ||
|
||
if not os.path.exists(attachment_path): | ||
raise FileNotFoundError(f"Attachment '{attachment_path}' not found.") | ||
|
||
message = Mail( | ||
from_email='[email protected]', # Sender epost api | ||
to_emails=to_emails, # Til epost som blir lagt inn, tror den er definert som "email" i koden. | ||
subject=subject, | ||
html_content=content | ||
) | ||
|
||
with open(attachment_path, 'rb') as f: | ||
data = f.read() | ||
encoded_file = base64.b64encode(data).decode() | ||
|
||
attachedFile = Attachment( | ||
FileContent(encoded_file), | ||
FileName(os.path.basename(attachment_path)), | ||
FileType('application/zip'), | ||
Disposition('attachment') | ||
) | ||
message.attachment = attachedFile | ||
|
||
try: | ||
sg = SendGridAPIClient(os.getenv('SENDGRID_API_KEY')) # Henter API nøkkel | ||
response = sg.send(message) | ||
# Prints response below | ||
print(f"Email sent. Status code: {response.status_code}") | ||
except Exception as e: | ||
print(f"An error occurred: {e}") | ||
|
||
def zip_files(directory_path: str = 'WMS/email/', zip_name: str = 'attachments.zip'): | ||
"""Zip all files in the specified directory and save them to a zip file.""" | ||
with ZipFile(zip_name, 'w') as zipf: | ||
for root, dirs, files in os.walk(directory_path): | ||
for file in files: | ||
file_path = os.path.join(root, file) | ||
zipf.write(file_path, arcname=os.path.relpath(file_path, directory_path)) | ||
|
||
@app.post("/send-email/") | ||
async def send_zipped_files_email(): | ||
"""Zip and send email to endpoint""" | ||
zip_files() # Zipper alle filer i WMS/email/ | ||
|
||
send_email_with_attachment( | ||
to_emails=["[email protected]"], | ||
subject="Here are your zipped files", | ||
content="<strong>Zip file holding the requested data.</strong>", | ||
attachment_path="attachments.zip" | ||
) | ||
|
||
# Sletter zip etter sending | ||
os.remove("attachments.zip") | ||
|
||
return {"message": "Email sent successfully with zipped files."} | ||
|
||
if __name__ == "__main__": | ||
import uvicorn | ||
uvicorn.run(app, host="127.0.0.1", port=8000) |
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