File tree Expand file tree Collapse file tree 12 files changed +513
-297
lines changed
android/library/src/main/java/io/appwrite
kotlin/src/main/kotlin/io/appwrite Expand file tree Collapse file tree 12 files changed +513
-297
lines changed Original file line number Diff line number Diff line change 1
1
package {{ sdk .namespace | caseDot }}
2
2
3
+ import java.time.Instant
4
+ import kotlin.math.floor
5
+ import kotlin.random.Random
6
+
7
+ fun uniqid(): String {
8
+ val currentTimestamp = Instant.now().toEpochMilli() / 1000.0
9
+ val secondsPart = floor(currentTimestamp).toLong()
10
+ val microsecondsPart = ((currentTimestamp - secondsPart) * 1_000_000).toLong()
11
+
12
+ // Convert both parts to hexadecimal format
13
+ val hexTimestamp = "%08x%05x".format(secondsPart, microsecondsPart)
14
+ return hexTimestamp
15
+ }
16
+
3
17
class ID {
4
18
companion object {
5
19
fun custom(id: String): String
6
20
= id
7
- fun unique(): String
8
- = "unique()"
21
+ fun unique(padding: Int = 5): String {
22
+ val baseId = uniqid()
23
+ val randomPadding = (1..padding)
24
+ .map { Random.nextInt(0, 16).toString(16) }
25
+ .joinToString("")
26
+
27
+ return baseId + randomPadding
28
+ }
9
29
}
10
- }
30
+ }
Original file line number Diff line number Diff line change @@ -4,13 +4,35 @@ part of {{ language.params.packageName }};
4
4
class ID {
5
5
ID._();
6
6
7
- /// Have Appwrite generate a unique ID for you.
8
- static String unique() {
9
- return 'unique()';
7
+ // Generate a unique ID based on timestamp
8
+ // Recreated from https://www.php.net/manual/en/function.uniqid.php
9
+ static String _uniqid() {
10
+ final now = DateTime.now();
11
+ final secondsSinceEpoch = (now.millisecondsSinceEpoch / 1000).floor();
12
+ final msecs = now.microsecondsSinceEpoch - secondsSinceEpoch * 1000000;
13
+ return secondsSinceEpoch.toRadixString(16) +
14
+ msecs.toRadixString(16).padLeft(5, '0');
15
+ }
16
+
17
+ // Generate a unique ID with padding to have a longer ID
18
+ // Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
19
+ static String unique({int padding = 7}) {
20
+ String id = _uniqid();
21
+
22
+ if (padding > 0) {
23
+ StringBuffer sb = StringBuffer();
24
+ for (var i = 0; i < padding; i++) {
25
+ sb.write(Random().nextInt(16).toRadixString(16));
26
+ }
27
+
28
+ id += sb.toString();
29
+ }
30
+
31
+ return id;
10
32
}
11
33
12
34
/// Uses [id] as the ID for the resource.
13
35
static String custom(String id) {
14
36
return id;
15
37
}
16
- }
38
+ }
Original file line number Diff line number Diff line change 1
1
export class ID {
2
+ function uniqid(): string {
3
+ const now = new Date();
4
+ const secondsSinceEpoch = Math.floor(now.getTime() / 1000);
5
+ const msecs = now.getTime() - secondsSinceEpoch * 1000;
6
+ const microsecondsPart = msecs * 1000; // Convert milliseconds to microseconds
7
+
8
+ // Convert to hexadecimal
9
+ const hexTimestamp = secondsSinceEpoch.toString(16) + microsecondsPart.toString(16).padStart(5, '0');
10
+ return hexTimestamp;
11
+ }
12
+
2
13
public static custom(id: string): string {
3
14
return id
4
15
}
5
-
6
- public static unique(): string {
7
- return 'unique()'
16
+
17
+ public static unique(padding: number = 5): string {
18
+ const baseId = uniqid();
19
+ let randomPadding = '';
20
+
21
+ for (let i = 0; i < padding; i++) {
22
+ const randomHexDigit = Math.floor(Math.random() * 16).toString(16);
23
+ randomPadding += randomHexDigit;
24
+ }
25
+
26
+ return baseId + randomPadding;
8
27
}
9
- }
28
+ }
Original file line number Diff line number Diff line change @@ -2,14 +2,35 @@ namespace {{ spec.title | caseUcfirst }}
2
2
{
3
3
public static class ID
4
4
{
5
- public static string Unique ()
5
+ static string UniqID ()
6
6
{
7
- return "unique()";
7
+ var now = DateTime.UtcNow;
8
+ var secondsSinceEpoch = (long)(now - new DateTime(1970, 1, 1)).TotalSeconds;
9
+ var msecs = (now - new DateTime(1970, 1, 1)).TotalMilliseconds - secondsSinceEpoch * 1000;
10
+ var microsecondsPart = (long)(msecs * 1000); // Convert milliseconds to microseconds
11
+
12
+ // Convert to hexadecimal
13
+ var hexTimestamp = secondsSinceEpoch.ToString("x") + microsecondsPart.ToString("x").PadLeft(5, '0');
14
+ return hexTimestamp;
15
+ }
16
+
17
+ public static string Unique(int padding = 5)
18
+ {
19
+ var baseId = UniqID();
20
+ var randomPadding = "";
21
+
22
+ for (int i = 0; i < padding; i++)
23
+ {
24
+ var randomHexDigit = random.Next(0, 16).ToString("x");
25
+ randomPadding += randomHexDigit;
26
+ }
27
+
28
+ return baseId + randomPadding;
8
29
}
9
30
10
31
public static string Custom(string id)
11
32
{
12
33
return id;
13
34
}
14
35
}
15
- }
36
+ }
Original file line number Diff line number Diff line change 1
1
package {{ sdk .namespace | caseDot }}
2
2
3
+ import java.time.Instant
4
+ import kotlin.math.floor
5
+ import kotlin.random.Random
6
+
7
+ fun uniqid(): String {
8
+ val currentTimestamp = Instant.now().toEpochMilli() / 1000.0
9
+ val secondsPart = floor(currentTimestamp).toLong()
10
+ val microsecondsPart = ((currentTimestamp - secondsPart) * 1_000_000).toLong()
11
+
12
+ // Convert both parts to hexadecimal format
13
+ val hexTimestamp = "%08x%05x".format(secondsPart, microsecondsPart)
14
+ return hexTimestamp
15
+ }
16
+
3
17
class ID {
4
18
companion object {
5
19
fun custom(id: String): String
6
20
= id
7
- fun unique(): String
8
- = "unique()"
21
+ fun unique(padding: Int = 5): String {
22
+ val baseId = uniqid()
23
+ val randomPadding = (1..padding)
24
+ .map { Random.nextInt(0, 16).toString(16) }
25
+ .joinToString("")
26
+
27
+ return baseId + randomPadding
28
+ }
9
29
}
10
- }
30
+ }
Original file line number Diff line number Diff line change 1
1
class ID {
2
+ static function uniqid() {
3
+ const now = new Date();
4
+ const secondsSinceEpoch = Math.floor(now.getTime() / 1000);
5
+ const msecs = now.getTime() - secondsSinceEpoch * 1000;
6
+ const microsecondsPart = msecs * 1000; // Convert milliseconds to microseconds
2
7
3
- static unique = () => {
4
- return 'unique()'
8
+ // Convert to hexadecimal
9
+ const hexTimestamp = secondsSinceEpoch.toString(16) + microsecondsPart.toString(16).padStart(5, '0');
10
+ return hexTimestamp;
11
+ }
12
+
13
+ static unique = (padding = 7) => {
14
+ const baseId = uniqid();
15
+ let randomPadding = '';
16
+
17
+ for (let i = 0; i < padding; i++) {
18
+ const randomHexDigit = Math.floor(Math.random() * 16).toString(16);
19
+ randomPadding += randomHexDigit;
20
+ }
21
+
22
+ return baseId + randomPadding;
5
23
}
6
24
7
25
static custom = (id) => {
Original file line number Diff line number Diff line change 3
3
namespace {{ spec . title | caseUcfirst }};
4
4
5
5
class ID {
6
- public static function unique (): string
6
+ public static function unique (int $padding = 5 ): string
7
7
{
8
- return ' unique()' ;
8
+ $uniqid = \uniqid ();
9
+
10
+ if ($padding > 0 ) {
11
+ $bytes = \random_bytes (\max (1 , (int )\ceil (($padding / 2 )))); // one byte expands to two chars
12
+ $uniqid .= \substr (\bin2hex ($bytes ), 0 , $padding );
13
+ }
14
+
15
+ return $uniqid ;
9
16
}
10
17
11
18
public static function custom (string $id ): string
12
19
{
13
20
return $id ;
14
21
}
15
- }
22
+ }
Original file line number Diff line number Diff line change
1
+ from datetime import datetime
2
+ import random
3
+
1
4
class ID:
5
+ @staticmethod
6
+ def uniqid():
7
+ now = datetime.now()
8
+ seconds_since_epoch = int(now.timestamp())
9
+ microseconds_part = now.microsecond
10
+ hex_timestamp = f'{seconds_since_epoch:08x}{microseconds_part:05x}'
11
+ return hex_timestamp
12
+
2
13
@staticmethod
3
14
def custom(id):
4
15
return id
5
16
6
17
@staticmethod
7
- def unique():
8
- return 'unique()'
18
+ def unique(padding = 5):
19
+ base_id = uniqid()
20
+ random_padding = ''.join(random.choice('0123456789abcdef') for _ in range(padding))
21
+ return base_id + random_padding
Original file line number Diff line number Diff line change
1
+ require 'securerandom'
2
+
3
+ def uniqid
4
+ now = Time.now
5
+ seconds_since_epoch = now.to_i
6
+ microseconds_part = now.usec
7
+ hex_timestamp = "%08x%05x" % [seconds_since_epoch, microseconds_part]
8
+ hex_timestamp
9
+ end
10
+
1
11
module {{spec .title | caseUcfirst }}
2
12
class ID
3
13
def self.custom(id)
4
14
id
5
15
end
6
-
7
- def self.unique
8
- 'unique()'
16
+
17
+ def self.unique_id(padding=5)
18
+ base_id = uniqid
19
+ random_padding = SecureRandom.hex(padding / 2)
20
+ random_padding = random_padding[0...padding]
21
+ base_id + random_padding
9
22
end
10
23
end
11
- end
24
+ end
You can’t perform that action at this time.
0 commit comments