akra35567 commited on
Commit
22dcb40
Β·
verified Β·
1 Parent(s): 8b8f916

Update modules/SubscriptionManager.js

Browse files
Files changed (1) hide show
  1. modules/SubscriptionManager.js +41 -9
modules/SubscriptionManager.js CHANGED
@@ -21,21 +21,50 @@ const path = require('path');
21
  class SubscriptionManager {
22
  constructor(config) {
23
  this.config = config;
24
- this.dataPath = path.join(config.DATABASE_FOLDER, 'subscriptions');
 
 
 
 
 
 
 
 
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
- if (!fs.existsSync(this.dataPath)) {
30
- fs.mkdirSync(this.dataPath, { recursive: true });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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._cleanOldUsage();
 
 
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.error(`Erro ao salvar ${filepath}:`, e);
 
 
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;