Spaces:
Running
Running
File size: 9,300 Bytes
3750964 6232012 3750964 9313ac7 3750964 b5229dd 3750964 c5936ad 3750964 6232012 c5936ad 3750964 c5936ad 3750964 c5936ad 3750964 09f04ee 3750964 6232012 c5936ad 3750964 09f04ee 3750964 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | <?php
namespace App\Models;
/**
* Lightweight JSON file store for push notification content and device
* subscriptions. Mirrors the storage pattern used by the book management
* feature (public/assets/books.json), but keeps site-managed data inside
* the writable storage directory so it works on HF Spaces, Vercel and local.
*
* The notifications file can optionally be synced to a Hugging Face bucket
* for durable storage and cross-deployment sharing (see syncToBucket()).
*/
class PushData
{
private function dataDir()
{
$dir = __DIR__ . '/../../storage/data';
if (!is_dir($dir)) {
@mkdir($dir, 0755, true);
}
return $dir;
}
/**
* Build a writable HF home and wrap a command so the hf/xet CLI can cache
* its logs/data there. Apache runs as www-data (not root), so the default
* /root/.cache/huggingface is not writable and hf cp fails with
* "Permission denied (os error 13)".
*/
private function hfCommand($args)
{
$home = getenv('HF_HOME') ?: ($_ENV['HF_HOME'] ?? ($this->dataDir() . '/.hfhome'));
@mkdir($home, 0777, true);
return 'HF_HOME=' . escapeshellarg($home)
. ' HF_TOKEN=' . escapeshellarg(getenv('HF_TOKEN') ?: ($_ENV['HF_TOKEN'] ?? ''))
. ' hf ' . $args . ' 2>&1';
}
private function notificationsPath()
{
return $this->dataDir() . '/notifications.json';
}
private function subscriptionsPath()
{
return $this->dataDir() . '/push-subscriptions.json';
}
private function load($path, $default = [])
{
if (!file_exists($path)) return $default;
$raw = @file_get_contents($path);
if ($raw === false) return $default;
$data = json_decode($raw, true);
return is_array($data) ? $data : $default;
}
private function save($path, $data)
{
file_put_contents($path, json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
}
/* ----------------------- notifications ----------------------- */
public function notifications()
{
return array_values($this->load($this->notificationsPath(), []));
}
public function findNotification($id)
{
foreach ($this->notifications() as $n) {
if (($n['id'] ?? '') === $id) return $n;
}
return null;
}
public function saveNotifications($notifications)
{
$this->save($this->notificationsPath(), array_values($notifications));
}
public function storeNotification($input)
{
$notifications = $this->notifications();
$id = !empty($input['id']) ? $input['id'] : uniqid('notif_', true);
$entry = [
'id' => $id,
'title' => trim($input['title'] ?? ''),
'body' => trim($input['body'] ?? ''),
'url' => trim($input['url'] ?? ''),
'icon' => trim($input['icon'] ?? ''),
'enabled' => !empty($input['enabled']),
'created_at' => $input['created_at'] ?? date('c'),
'updated_at' => date('c'),
];
// Update in place if editing an existing record
foreach ($notifications as $i => &$n) {
if (($n['id'] ?? '') === $id) {
$entry['created_at'] = $n['created_at'] ?? $entry['created_at'];
unset($n);
$notifications[$i] = $entry;
$this->saveNotifications($notifications);
return ['success' => true, 'notification' => $entry];
}
}
unset($n);
$notifications[] = $entry;
$this->saveNotifications($notifications);
return ['success' => true, 'notification' => $entry];
}
public function destroyNotification($id)
{
$notifications = $this->notifications();
$filtered = array_values(array_filter($notifications, function ($n) use ($id) {
return ($n['id'] ?? '') !== $id;
}));
if (count($filtered) === count($notifications)) {
return ['success' => false, 'error' => 'Notification not found'];
}
$this->saveNotifications($filtered);
return ['success' => true];
}
public function destroyNotificationByTitle($title)
{
if (!$title) return ['success' => false, 'error' => 'Missing title'];
$notifications = $this->notifications();
$filtered = array_values(array_filter($notifications, function ($n) use ($title) {
return ($n['title'] ?? '') !== $title;
}));
if (count($filtered) === count($notifications)) {
return ['success' => false, 'error' => 'Notification not found'];
}
$this->saveNotifications($filtered);
return ['success' => true];
}
/* --------------------- subscriptions --------------------- */
public function subscriptions()
{
return array_values($this->load($this->subscriptionsPath(), []));
}
public function saveSubscriptions($subs)
{
$this->save($this->subscriptionsPath(), array_values($subs));
}
public function addSubscription($sub)
{
$subs = $this->subscriptions();
$endpoint = $sub['endpoint'] ?? '';
foreach ($subs as $i => &$s) {
if (($s['endpoint'] ?? '') === $endpoint) {
$sub['created_at'] = $s['created_at'] ?? date('c');
$sub['updated_at'] = date('c');
$subs[$i] = $sub;
$this->saveSubscriptions($subs);
return ['success' => true];
}
}
unset($s);
$sub['created_at'] = date('c');
$sub['updated_at'] = date('c');
$subs[] = $sub;
$this->saveSubscriptions($subs);
return ['success' => true];
}
public function removeSubscription($endpoint)
{
$subs = $this->subscriptions();
$filtered = array_values(array_filter($subs, function ($s) use ($endpoint) {
return ($s['endpoint'] ?? '') !== $endpoint;
}));
$this->saveSubscriptions($filtered);
return ['success' => true];
}
/* ------------------- HF bucket sync ------------------- */
/**
* Best-effort push of the notifications (and subscriptions) files to a
* Hugging Face bucket. Requires the hf CLI and a valid HF_TOKEN with
* write access to the bucket in the running environment.
*/
public function syncToBucket()
{
$bucket = getenv('HF_BUCKET') ?: ($_ENV['HF_BUCKET'] ?? '');
$path = getenv('HF_BUCKET_PATH') ?: ($_ENV['HF_BUCKET_PATH'] ?? 'notifications');
$dataDir = $this->dataDir();
$canWrite = $this->canWriteToBucket();
if (!$bucket || !$canWrite) {
return ['success' => false, 'synced' => false, 'reason' => 'bucket_not_configured'];
}
$uploads = [
'notifications.json' => "$path/notifications.json",
'push-subscriptions.json' => "$path/push-subscriptions.json",
];
$synced = [];
$errors = [];
foreach ($uploads as $file => $target) {
$src = "$dataDir/$file";
if (!file_exists($src)) continue;
$cmd = $this->hfCommand('cp ' . escapeshellarg($src) . ' hf://buckets/' . $bucket . '/' . escapeshellarg($target));
$out = [];
$code = 0;
exec($cmd, $out, $code);
if ($code === 0) {
$synced[] = $file;
} else {
$errors[$file] = implode("\n", $out);
}
}
return [
'success' => count($synced) > 0,
'synced' => count($synced) > 0,
'files' => $synced,
'bucket' => "$bucket/$path",
'errors' => $errors,
];
}
/**
* Best-effort pull (download) of the notifications file from the bucket
* into local storage, so a fresh deployment can load managed content.
*/
public function pullFromBucket()
{
$bucket = getenv('HF_BUCKET') ?: ($_ENV['HF_BUCKET'] ?? '');
$path = getenv('HF_BUCKET_PATH') ?: ($_ENV['HF_BUCKET_PATH'] ?? 'notifications');
$dataDir = $this->dataDir();
$canWrite = $this->canWriteToBucket();
if (!$bucket || !$canWrite) {
return ['success' => false, 'reason' => 'bucket_not_configured'];
}
$target = "$path/notifications.json";
$local = "$dataDir/notifications.json";
$cmd = $this->hfCommand('cp hf://buckets/' . $bucket . '/' . escapeshellarg($target) . ' ' . escapeshellarg($local));
$out = [];
$code = 0;
exec($cmd, $out, $code);
return [
'success' => $code === 0 && file_exists($local),
'output' => implode("\n", $out),
'exit' => $code,
'bucket' => "$bucket/$target",
'local' => $local,
];
}
private function canWriteToBucket()
{
// Only attempt bucket writes when an explicit token is present.
// The deployed Space passes HF_TOKEN as a secret.
if (getenv('HF_TOKEN')) return true;
if (isset($_ENV['HF_TOKEN']) && $_ENV['HF_TOKEN']) return true;
return false;
}
}
|