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

Feat: storage_cleaner in Dart #256

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions dart/storage_cleaner/.gitignore
Original file line number Diff line number Diff line change
@@ -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
48 changes: 48 additions & 0 deletions dart/storage_cleaner/README.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions dart/storage_cleaner/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:lints/recommended.yaml
37 changes: 37 additions & 0 deletions dart/storage_cleaner/lib/appwrite_service.dart
Original file line number Diff line number Diff line change
@@ -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);
}
}
18 changes: 18 additions & 0 deletions dart/storage_cleaner/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'dart:async';
import 'dart:io';
import 'appwrite_service.dart';
import 'utils.dart';

Future<dynamic> 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);
}
26 changes: 26 additions & 0 deletions dart/storage_cleaner/lib/utils.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> obj, List<String> keys) {
final missing = <String>[];
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(', ')}');
}
}
11 changes: 11 additions & 0 deletions dart/storage_cleaner/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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