From f7075c9e20b5f30b094968b22c5075f6b2356a99 Mon Sep 17 00:00:00 2001 From: Shiba Kar Date: Sun, 5 Nov 2023 22:45:57 +0530 Subject: [PATCH] final --- dart/storage_cleaner/.gitignore | 27 +++++++++++ dart/storage_cleaner/README.md | 48 +++++++++++++++++++ dart/storage_cleaner/analysis_options.yaml | 1 + .../storage_cleaner/lib/appwrite_service.dart | 37 ++++++++++++++ dart/storage_cleaner/lib/main.dart | 18 +++++++ dart/storage_cleaner/lib/utils.dart | 26 ++++++++++ dart/storage_cleaner/pubspec.yaml | 11 +++++ 7 files changed, 168 insertions(+) create mode 100644 dart/storage_cleaner/.gitignore create mode 100644 dart/storage_cleaner/README.md create mode 100644 dart/storage_cleaner/analysis_options.yaml create mode 100644 dart/storage_cleaner/lib/appwrite_service.dart create mode 100644 dart/storage_cleaner/lib/main.dart create mode 100644 dart/storage_cleaner/lib/utils.dart create mode 100644 dart/storage_cleaner/pubspec.yaml diff --git a/dart/storage_cleaner/.gitignore b/dart/storage_cleaner/.gitignore new file mode 100644 index 00000000..dc259f50 --- /dev/null +++ b/dart/storage_cleaner/.gitignore @@ -0,0 +1,27 @@ +# See https://www.dartlang.org/guides/libraries/private-files + +# Files and directories created by pub +.dart_tool/ +.packages +build/ +# If you're building an application, you may want to check-in your pubspec.lock +pubspec.lock + +# Directory created by dartdoc +# If you don't generate documentation locally you can remove this line. +doc/api/ + +# dotenv environment variables file +.env* + +# Avoid committing generated Javascript files: +*.dart.js +*.info.json # Produced by the --dump-info flag. +*.js # When generated by dart2js. Don't specify *.js if your + # project includes source files written in JavaScript. +*.js_ +*.js.deps +*.js.map + +.flutter-plugins +.flutter-plugins-dependencies \ No newline at end of file diff --git a/dart/storage_cleaner/README.md b/dart/storage_cleaner/README.md new file mode 100644 index 00000000..f77a225e --- /dev/null +++ b/dart/storage_cleaner/README.md @@ -0,0 +1,48 @@ +# ⚡ Dart Starter Function + +A simple starter function. Edit `lib/main.dart` to get started and create something awesome! 🚀 + +## 🧰 Usage + +### GET / + +- Returns a "Hello, World!" message. + +**Response** + +Sample `200` Response: + +```text +Hello, World! +``` + +### POST, PUT, PATCH, DELETE / + +- Returns a "Learn More" JSON response. + +**Response** + +Sample `200` Response: + +```json +{ + "motto": "Build like a team of hundreds_", + "learn": "https://appwrite.io/docs", + "connect": "https://appwrite.io/discord", + "getInspired": "https://builtwith.appwrite.io" +} +``` + +## ⚙️ Configuration + +| Setting | Value | +|-------------------|-----------------| +| Runtime | Dart (2.17) | +| Entrypoint | `lib/main.dart` | +| Build Commands | `dart pub get` | +| Permissions | `any` | +| Timeout (Seconds) | 15 | + +## 🔒 Environment Variables + +No environment variables required. diff --git a/dart/storage_cleaner/analysis_options.yaml b/dart/storage_cleaner/analysis_options.yaml new file mode 100644 index 00000000..ea2c9e94 --- /dev/null +++ b/dart/storage_cleaner/analysis_options.yaml @@ -0,0 +1 @@ +include: package:lints/recommended.yaml \ No newline at end of file diff --git a/dart/storage_cleaner/lib/appwrite_service.dart b/dart/storage_cleaner/lib/appwrite_service.dart new file mode 100644 index 00000000..0bfc05a3 --- /dev/null +++ b/dart/storage_cleaner/lib/appwrite_service.dart @@ -0,0 +1,37 @@ +import 'dart:io'; + +import 'package:dart_appwrite/dart_appwrite.dart'; +import 'package:dart_appwrite/models.dart'; +import 'utils.dart'; + +class AppwriteService { + late Client client; + late Storage storage; + AppwriteService() { + client = Client() + ..setEndpoint(Platform.environment['APPWRITE_ENDPOINT'] ?? + 'https://cloud.appwrite.io/v1') + ..setProject(Platform.environment['APPWRITE_FUNCTION_PROJECT_ID']) + ..setKey(Platform.environment['APPWRITE_API_KEY']); + + storage = Storage(client); + } + + Future cleanBucket(String bucketId) async { + FileList response; + var queries = [ + Query.lessThan('\$createdAt', getExpiryDate()), + Query.limit(25), + ]; + + do { + response = await storage.listFiles(bucketId: bucketId, queries: queries); + + await Future.wait( + response.files.map( + (file) => storage.deleteFile(bucketId: bucketId, fileId: file.$id), + ), + ); + } while (response.files.isNotEmpty); + } +} diff --git a/dart/storage_cleaner/lib/main.dart b/dart/storage_cleaner/lib/main.dart new file mode 100644 index 00000000..1d16479e --- /dev/null +++ b/dart/storage_cleaner/lib/main.dart @@ -0,0 +1,18 @@ +import 'dart:async'; +import 'dart:io'; +import 'appwrite_service.dart'; +import 'utils.dart'; + +Future main(final context) async { + throwIfMissing(Platform.environment, [ + 'APPWRITE_API_KEY', + 'RETENTION_PERIOD_DAYS', + 'APPWRITE_BUCKET_ID', + ]); + + var appwrite = AppwriteService(); + + await appwrite.cleanBucket(Platform.environment['APPWRITE_BUCKET_ID']!); + + return context.res.send('Buckets cleaned', 200); +} diff --git a/dart/storage_cleaner/lib/utils.dart b/dart/storage_cleaner/lib/utils.dart new file mode 100644 index 00000000..c163a0a5 --- /dev/null +++ b/dart/storage_cleaner/lib/utils.dart @@ -0,0 +1,26 @@ +import 'dart:io'; + +String getExpiryDate() { + final retentionPeriod = + int.parse(Platform.environment['RETENTION_PERIOD_DAYS'] ?? "30"); + final now = DateTime.now(); + final expiryDate = now.subtract(Duration(days: retentionPeriod)); + + return expiryDate.toIso8601String(); +} + +/// Throws an error if any of the keys are missing from the object +/// @param obj - The object to check +/// @param keys - The list of keys to check for +/// @throws Exception +void throwIfMissing(Map obj, List keys) { + final missing = []; + for (var key in keys) { + if (!obj.containsKey(key) || obj[key] == null) { + missing.add(key); + } + } + if (missing.isNotEmpty) { + throw Exception('Missing required fields: ${missing.join(', ')}'); + } +} diff --git a/dart/storage_cleaner/pubspec.yaml b/dart/storage_cleaner/pubspec.yaml new file mode 100644 index 00000000..315f9448 --- /dev/null +++ b/dart/storage_cleaner/pubspec.yaml @@ -0,0 +1,11 @@ +name: storage_cleaner +version: 1.0.0 + +environment: + sdk: ^2.17.0 + +dependencies: + dart_appwrite: 8.0.0 + +dev_dependencies: + lints: ^2.0.0