@@ -23,12 +23,30 @@ class BroadcastManager
2323 private API $ api ;
2424 private ?array $ currentBroadcastState = null ;
2525 private array $ albumTimers = [];
26+ private static string $ dataDir ;
2627
2728 public function __construct (API $ api )
2829 {
2930 $ this ->api = $ api ;
3031 }
3132
33+ /**
34+ * Set data dir
35+ */
36+ public static function setDataDir (string $ path ): void {
37+ self ::$ dataDir = rtrim ($ path , '/ ' );
38+ }
39+
40+ /**
41+ * Get data dir
42+ */
43+ private static function getDataDir (): string {
44+ if (!self ::$ dataDir ) {
45+ self ::$ dataDir = __DIR__ . '/../data ' ;
46+ }
47+ return self ::$ dataDir ;
48+ }
49+
3250 /**
3351 * Broadcast messages to users/channels with progress tracking
3452 */
@@ -268,12 +286,12 @@ public function broadcastWithProgress(
268286 ($ state ['cancel ' ] ? "🛑 <b>Cancelled</b> " : "✅ <b>Finished</b> " );
269287
270288 try { $ api ->messages ->editMessage (['peer ' =>$ chatId ,'id ' =>$ statusId ,'message ' =>$ finalText ,'parse_mode ' =>'HTML ' ]); } catch (\Throwable ) {}
271- $ dir1 =__DIR__ . " /data " ;
289+ $ dir1 = self :: getDataDir () ;
272290 if (!is_dir ($ dir1 ))@mkdir ($ dir1 ,0777 ,true );
273- try { \Amp \File \write (__DIR__ . "$ dir1/LastBrodDATA.txt " ,$ finalText ); } catch (\Throwable ) {}
291+ try { \Amp \File \write ("$ dir1/LastBrodDATA.txt " ,$ finalText ); } catch (\Throwable ) {}
274292
275293 foreach ($ state ['lastMessageIds ' ] as $ peer =>$ id ) {
276- $ dir= __DIR__ . " /data /$ peer " ;
294+ $ dir = self :: getDataDir () . " / $ peer " ;
277295 if (!is_dir ($ dir ))@mkdir ($ dir ,0777 ,true );
278296 try {
279297 $ fh = \Amp \File \openFile ("$ dir/messages.txt " , "a " );
@@ -403,7 +421,7 @@ public function deleteLastBroadcastForAll(
403421 $ state ['inFlight ' ][$ peer ] = $ job ;
404422
405423 try {
406- $ file = __DIR__ . " /data /$ peer/lastBroadcast.txt " ;
424+ $ file = self :: getDataDir () . " / $ peer/lastBroadcast.txt " ;
407425 if (!file_exists ($ file )) {
408426 $ state ['failed ' ]++;
409427 unset($ state ['inFlight ' ][$ peer ]);
@@ -760,7 +778,7 @@ public function deleteAllBroadcastsForAll(
760778 $ userDeleted = false ;
761779
762780 try {
763- $ file = __DIR__ . " /data /$ peer/messages.txt " ;
781+ $ file = self :: getDataDir () . " / $ peer/messages.txt " ;
764782 if (!file_exists ($ file )) {
765783 $ state ['failed ' ]++;
766784 unset($ state ['inFlight ' ][$ peer ]);
@@ -808,7 +826,7 @@ public function deleteAllBroadcastsForAll(
808826 if ($ userDeleted ) $ state ['deleted ' ]++;
809827 else $ state ['failed ' ]++;
810828
811- $ file2 = __DIR__ . " /data /$ peer/lastBroadcast.txt " ;
829+ $ file2 = self :: getDataDir () . " / $ peer/lastBroadcast.txt " ;
812830 @unlink ($ file2 );
813831 @unlink ($ file );
814832 unset($ state ['inFlight ' ][$ peer ]);
@@ -1091,10 +1109,13 @@ public function unpinAllMessagesForAll(
10911109 return $ state ;
10921110}
10931111
1112+ /**
1113+ * Progress bar
1114+ */
10941115 private function progressBar (int $ current , int $ total ): string {
10951116 $ len = 20 ;
1096- $ filled = (int ) round ($ current / max ($ total ,1 ) * $ len );
1097- return str_repeat ('█ ' ,$ filled ).str_repeat ('░ ' ,$ len -$ filled ).' ' .round (($ current /$ total )*100 ).'% ' ;
1117+ $ filled = (int ) round ($ current / max ($ total ,1 ) * $ len );
1118+ return str_repeat ('█ ' ,$ filled ).str_repeat ('░ ' ,$ len -$ filled ).' ' .round (($ current /max ( $ total, 1 ) )*100 ).'% ' ;
10981119 }
10991120
11001121 /**
@@ -1142,7 +1163,7 @@ public function isCancelled(): bool {
11421163 * Check if there is a last broadcast message saved for deletion
11431164 */
11441165 public function hasLastBroadcast (): bool {
1145- foreach (glob (__DIR__ . " /data /*/lastBroadcast.txt " ) as $ file ) {
1166+ foreach (glob (self :: getDataDir () . " /*/lastBroadcast.txt " ) as $ file ) {
11461167 if (file_exists ($ file ) && trim (\Amp \File \read ($ file ))) {
11471168 return true ;
11481169 }
@@ -1154,7 +1175,7 @@ public function hasLastBroadcast(): bool {
11541175 * Check if there is a broadcast messages saved for deletion
11551176 */
11561177 public function hasAllBroadcast (): bool {
1157- foreach (glob (__DIR__ . " /data /*/messages.txt " ) as $ file ) {
1178+ foreach (glob (self :: getDataDir () . " /*/messages.txt " ) as $ file ) {
11581179 if (file_exists ($ file ) && trim (\Amp \File \read ($ file ))) {
11591180 return true ;
11601181 }
@@ -1187,8 +1208,8 @@ public function progress(): ?array {
11871208 * Last broadcast data
11881209 */
11891210 public function lastBroadcastData (): string |false {
1190- if (file_exists (__DIR__ . " /data /LastBrodDATA.txt " )) {
1191- return \Amp \File \read (__DIR__ . " /data /LastBrodDATA.txt " );
1211+ if (file_exists (self :: getDataDir () . " /LastBrodDATA.txt " )) {
1212+ return \Amp \File \read (self :: getDataDir () . " /LastBrodDATA.txt " );
11921213 }else {
11931214 return false ;
11941215 }
0 commit comments