Update modules/SubscriptionManager.js
Browse files
modules/SubscriptionManager.js
CHANGED
|
@@ -21,21 +21,50 @@ const path = require('path');
|
|
| 21 |
class SubscriptionManager {
|
| 22 |
constructor(config) {
|
| 23 |
this.config = config;
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
this.usagePath = path.join(this.dataPath, 'usage.json');
|
| 26 |
this.subscribersPath = path.join(this.dataPath, 'subscribers.json');
|
| 27 |
|
| 28 |
-
// Cria diretΓ³rio se nΓ£o existir
|
| 29 |
-
|
| 30 |
-
fs.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
}
|
| 32 |
|
| 33 |
// Carrega dados
|
| 34 |
-
this.subscribers = this._loadJSON(this.subscribersPath, {});
|
| 35 |
-
this.usage = this._loadJSON(this.usagePath, {});
|
| 36 |
|
| 37 |
// Limpa uso antigo periodicamente
|
| 38 |
-
this.
|
|
|
|
|
|
|
| 39 |
|
| 40 |
console.log('β
SubscriptionManager inicializado');
|
| 41 |
}
|
|
@@ -337,10 +366,13 @@ class SubscriptionManager {
|
|
| 337 |
_saveJSON(filepath, data) {
|
| 338 |
try {
|
| 339 |
fs.writeFileSync(filepath, JSON.stringify(data, null, 2));
|
|
|
|
| 340 |
} catch (e) {
|
| 341 |
-
console.
|
|
|
|
|
|
|
| 342 |
}
|
| 343 |
}
|
| 344 |
}
|
| 345 |
|
| 346 |
-
module.exports = SubscriptionManager;
|
|
|
|
| 21 |
class SubscriptionManager {
|
| 22 |
constructor(config) {
|
| 23 |
this.config = config;
|
| 24 |
+
|
| 25 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 26 |
+
// HF SPACES: Usar /tmp para garantir permissΓ΅es de escrita
|
| 27 |
+
// O HF Spaces tem sistema de arquivos somente-leitura em /
|
| 28 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 29 |
+
|
| 30 |
+
// ForΓ§ar uso de /tmp no HF Spaces (sistema read-only)
|
| 31 |
+
this.dataPath = '/tmp/akira_data/subscriptions';
|
| 32 |
+
|
| 33 |
this.usagePath = path.join(this.dataPath, 'usage.json');
|
| 34 |
this.subscribersPath = path.join(this.dataPath, 'subscribers.json');
|
| 35 |
|
| 36 |
+
// Cria diretΓ³rio se nΓ£o existir - COM TRATAMENTO DE ERRO
|
| 37 |
+
try {
|
| 38 |
+
if (!fs.existsSync(this.dataPath)) {
|
| 39 |
+
fs.mkdirSync(this.dataPath, { recursive: true });
|
| 40 |
+
console.log(`β
SubscriptionManager: DiretΓ³rio criado: ${this.dataPath}`);
|
| 41 |
+
}
|
| 42 |
+
} catch (error) {
|
| 43 |
+
console.warn(`β οΈ SubscriptionManager: NΓ£o foi possΓvel criar diretΓ³rio em ${this.dataPath}:`, error.message);
|
| 44 |
+
|
| 45 |
+
// Fallback para /tmp direto se falhar
|
| 46 |
+
const tmpPath = '/tmp/subscriptions';
|
| 47 |
+
try {
|
| 48 |
+
fs.mkdirSync(tmpPath, { recursive: true });
|
| 49 |
+
this.dataPath = tmpPath;
|
| 50 |
+
this.usagePath = path.join(this.dataPath, 'usage.json');
|
| 51 |
+
this.subscribersPath = path.join(this.dataPath, 'subscribers.json');
|
| 52 |
+
console.log(`β
SubscriptionManager: Usando fallback: ${this.dataPath}`);
|
| 53 |
+
} catch (fallbackError) {
|
| 54 |
+
console.error('β SubscriptionManager: Erro crΓtico ao criar diretΓ³rio de fallback:', fallbackError.message);
|
| 55 |
+
// Continuar sem diretΓ³rio - usar memΓ³ria apenas
|
| 56 |
+
this.dataPath = null;
|
| 57 |
+
}
|
| 58 |
}
|
| 59 |
|
| 60 |
// Carrega dados
|
| 61 |
+
this.subscribers = this.dataPath ? this._loadJSON(this.subscribersPath, {}) : {};
|
| 62 |
+
this.usage = this.dataPath ? this._loadJSON(this.usagePath, {}) : {};
|
| 63 |
|
| 64 |
// Limpa uso antigo periodicamente
|
| 65 |
+
if (this.dataPath) {
|
| 66 |
+
this._cleanOldUsage();
|
| 67 |
+
}
|
| 68 |
|
| 69 |
console.log('β
SubscriptionManager inicializado');
|
| 70 |
}
|
|
|
|
| 366 |
_saveJSON(filepath, data) {
|
| 367 |
try {
|
| 368 |
fs.writeFileSync(filepath, JSON.stringify(data, null, 2));
|
| 369 |
+
return true;
|
| 370 |
} catch (e) {
|
| 371 |
+
console.warn(`Erro ao salvar ${filepath}:`, e);
|
| 372 |
+
// Se falhar, salvar em memΓ³ria apenas
|
| 373 |
+
return false;
|
| 374 |
}
|
| 375 |
}
|
| 376 |
}
|
| 377 |
|
| 378 |
+
module.exports = SubscriptionManager;
|