Browse Source

Migrate to storing torrent files on the filesystem rather than the DB

spaghetti 4 years ago
parent
commit
ba83225b19

+ 4
- 3
classes/config.template View File

@@ -5,9 +5,10 @@ if (version_compare(PHP_VERSION, '7.0.0', '<')) {
5 5
 //date_default_timezone_set('EST');
6 6
 
7 7
 // Main settings
8
-define('SITE_NAME',   'Oppaitime'); //The name of your site
9
-define('SITE_DOMAIN', 'oppaiti.me'); //The FQDN of your site
10
-define('SERVER_ROOT', '/var/www'); //The root of the server, used for includes, purpose is to shorten the path string
8
+define('SITE_NAME',     'Oppaitime'); //The name of your site
9
+define('SITE_DOMAIN',   'oppaiti.me'); //The FQDN of your site
10
+define('SERVER_ROOT',   '/var/www'); //The root of the server, used for includes
11
+define('TORRENT_STORE', '/var/torrents/'); //Where torrent files are stored
11 12
 
12 13
 // Tracker urls to be added to torrent files ala bittorrent.org/beps/bep_0012.html
13 14
 define('ANNOUNCE_URLS', [[

+ 6
- 10
classes/torrents.class.php View File

@@ -324,9 +324,7 @@ class Torrents {
324 324
       G::$Cache->decrement('num_torrent_reportsv2', $Reports);
325 325
     }
326 326
 
327
-    G::$DB->query("
328
-      DELETE FROM torrents_files
329
-      WHERE TorrentID = '$ID'");
327
+    unlink(TORRENT_STORE.$ID.'.torrent');
330 328
     G::$DB->query("
331 329
       DELETE FROM torrents_bad_tags
332 330
       WHERE TorrentID = $ID");
@@ -567,14 +565,12 @@ class Torrents {
567 565
     $QueryID = G::$DB->get_query_id();
568 566
 
569 567
     G::$DB->query("
570
-      SELECT tg.ID,
571
-        tf.File
572
-      FROM torrents_files AS tf
573
-        JOIN torrents AS t ON t.ID = tf.TorrentID
574
-        JOIN torrents_group AS tg ON tg.ID = t.GroupID
575
-      WHERE tf.TorrentID = $TorrentID");
568
+      SELECT GroupID
569
+      FROM torrents
570
+      WHERE ID = $TorrentID");
576 571
     if (G::$DB->has_results()) {
577
-      list($GroupID, $Contents) = G::$DB->next_record(MYSQLI_NUM, false);
572
+      list($GroupID) = G::$DB->next_record(MYSQLI_NUM, false);
573
+      $Contents = file_get_contents(TORRENT_STORE.$TorrentID.'.torrent');
578 574
       if (Misc::is_new_torrent($Contents)) {
579 575
         $Tor = new BencodeTorrent($Contents);
580 576
         $FilePath = (isset($Tor->Dec['info']['files']) ? Format::make_utf8($Tor->get_name()) : '');

+ 0
- 6
gazelle.sql View File

@@ -1181,12 +1181,6 @@ CREATE TABLE `torrents_bad_tags` (
1181 1181
   KEY `TimeAdded` (`TimeAdded`)
1182 1182
 ) ENGINE=InnoDB CHARSET=utf8;
1183 1183
 
1184
-CREATE TABLE `torrents_files` (
1185
-  `TorrentID` int(10) NOT NULL,
1186
-  `File` mediumblob NOT NULL,
1187
-  PRIMARY KEY (`TorrentID`)
1188
-) ENGINE=MyISAM DEFAULT CHARSET=utf8;
1189
-
1190 1184
 CREATE TABLE `torrents_group` (
1191 1185
   `ID` int(10) NOT NULL AUTO_INCREMENT,
1192 1186
   `CategoryID` int(10) DEFAULT NULL,

+ 3
- 15
sections/artist/download.php View File

@@ -44,7 +44,6 @@ SELECT
44 44
 FROM torrents AS t
45 45
   INNER JOIN torrents_group AS tg ON tg.ID = t.GroupID AND tg.CategoryID = '1'
46 46
   INNER JOIN artists_group AS a ON a.ArtistID = tg.ArtistID AND a.ArtistID = '59721'
47
-  LEFT JOIN torrents_files AS f ON t.ID = f.TorrentID
48 47
 ORDER BY t.GroupID ASC, Rank DESC, t.Seeders ASC
49 48
 */
50 49
 
@@ -142,20 +141,9 @@ $DownloadsQ = $DB->query($SQL);
142 141
 $Collector = new TorrentsDL($DownloadsQ, $ArtistName);
143 142
 while (list($Downloads, $GroupIDs) = $Collector->get_downloads('GroupID')) {
144 143
   $Artists = Artists::get_artists($GroupIDs);
145
-  $TorrentFilesQ = $DB->query("
146
-    SELECT TorrentID, File
147
-    FROM torrents_files
148
-    WHERE TorrentID IN (".implode(',', array_keys($GroupIDs)).')', false);
149
-  if (is_int($TorrentFilesQ)) {
150
-    // Query failed. Let's not create a broken zip archive
151
-    foreach ($GroupIDs as $GroupID) {
152
-      $Download =& $Downloads[$GroupID];
153
-      $Download['Artist'] = Artists::display_artists($Artists[$GroupID], false, true, false);
154
-      $Collector->fail_file($Download);
155
-    }
156
-    continue;
157
-  }
158
-  while (list($TorrentID, $TorrentFile) = $DB->next_record(MYSQLI_NUM, false)) {
144
+  $TorrentIDs = array_keys($GroupIDs);
145
+  foreach ($TorrentIDs as $TorrentID) {
146
+    $TorrentFile = file_get_contents(TORRENT_STORE.$TorrentID.'.torrent');
159 147
     $GroupID = $GroupIDs[$TorrentID];
160 148
     $Download =& $Downloads[$GroupID];
161 149
     $Download['Artist'] = Artists::display_artists($Artists[$Download['GroupID']], false, true, false);

+ 3
- 15
sections/collages/download.php View File

@@ -44,7 +44,6 @@ FROM torrents AS t
44 44
   INNER JOIN collages_torrents AS c ON t.GroupID = c.GroupID AND c.CollageID = '8'
45 45
   INNER JOIN torrents_group AS tg ON tg.ID = t.GroupID AND tg.CategoryID = '1'
46 46
   LEFT JOIN artists_group AS a ON a.ArtistID = tg.ArtistID
47
-  LEFT JOIN torrents_files AS f ON t.ID = f.TorrentID
48 47
 ORDER BY t.GroupID ASC, Rank DESC, t.Seeders ASC
49 48
 */
50 49
 
@@ -133,20 +132,9 @@ $Collector = new TorrentsDL($DownloadsQ, $CollageName);
133 132
 
134 133
 while (list($Downloads, $GroupIDs) = $Collector->get_downloads('GroupID')) {
135 134
   $Artists = Artists::get_artists($GroupIDs);
136
-  $TorrentFilesQ = $DB->query("
137
-    SELECT TorrentID, File
138
-    FROM torrents_files
139
-    WHERE TorrentID IN (".implode(',', array_keys($GroupIDs)).')', false);
140
-  if (is_int($TorrentFilesQ)) {
141
-    // Query failed. Let's not create a broken zip archive
142
-    foreach ($GroupIDs as $GroupID) {
143
-      $Download =& $Downloads[$GroupID];
144
-      $Download['Artist'] = Artists::display_artists($Artists[$GroupID], false, true, false);
145
-      $Collector->fail_file($Download);
146
-    }
147
-    continue;
148
-  }
149
-  while (list($TorrentID, $TorrentFile) = $DB->next_record(MYSQLI_NUM, false)) {
135
+  $TorrentIDs = array_keys($GroupIDs);
136
+  foreach ($TorrentIDs as $TorrentID) {
137
+    file_get_contents(TORRENT_STORE.$TorrentID.'.torrent');
150 138
     $GroupID = $GroupIDs[$TorrentID];
151 139
     $Download =& $Downloads[$GroupID];
152 140
     $Download['Artist'] = Artists::display_artists($Artists[$Download['GroupID']], false, true, false);

+ 2
- 4
sections/torrents/browse.php View File

@@ -582,8 +582,7 @@ $ShowGroups = !(!empty($LoggedUser['TorrentGrouping']) && $LoggedUser['TorrentGr
582 582
       $SnatchedTorrentClass = $Data['IsSnatched'] ? ' snatched_torrent' : '';
583 583
       $TorrentDL = "torrents.php?action=download&amp;id=".$TorrentID."&amp;authkey=".$LoggedUser['AuthKey']."&amp;torrent_pass=".$LoggedUser['torrent_pass'];
584 584
       if (!($TorrentFileName = $Cache->get_value('torrent_file_name_'.$TorrentID))) {
585
-        $DB->query("SELECT File FROM torrents_files WHERE TorrentID=".$TorrentID);
586
-        list($TorrentFile) = $DB->next_record(MYSQLI_NUM, false);
585
+        $TorrentFile = file_get_contents(TORRENT_STORE.$TorrentID.'.torrent');
587 586
         $Tor = new BencodeTorrent($TorrentFile);
588 587
         $TorrentFileName = $Tor->Dec['info']['name'];
589 588
         $Cache->cache_value('torrent_file_name_'.$TorrentID, $TorrentFileName);
@@ -655,8 +654,7 @@ $ShowGroups = !(!empty($LoggedUser['TorrentGrouping']) && $LoggedUser['TorrentGr
655 654
     $SnatchedTorrentClass = $Data['IsSnatched'] ? ' snatched_torrent' : '';
656 655
     $TorrentDL = "torrents.php?action=download&amp;id=".$TorrentID."&amp;authkey=".$LoggedUser['AuthKey']."&amp;torrent_pass=".$LoggedUser['torrent_pass'];
657 656
     if (!($TorrentFileName = $Cache->get_value('torrent_file_name_'.$TorrentID))) {
658
-      $DB->query("SELECT File FROM torrents_files WHERE TorrentID=".$TorrentID);
659
-      list($TorrentFile) = $DB->next_record(MYSQLI_NUM, false);
657
+      $TorrentFile = file_get_contents(TORRENT_STORE.$TorrentID.'.torrent');
660 658
       $Tor = new BencodeTorrent($TorrentFile);
661 659
       $TorrentFileName = $Tor->Dec['info']['name'];
662 660
       $Cache->cache_value('torrent_file_name_'.$TorrentID, $TorrentFileName);

+ 1
- 2
sections/torrents/details.php View File

@@ -516,8 +516,7 @@ foreach ($TorrentList as $Torrent) {
516 516
 
517 517
   $TorrentDL = "torrents.php?action=download&amp;id=".$TorrentID."&amp;authkey=".$LoggedUser['AuthKey']."&amp;torrent_pass=".$LoggedUser['torrent_pass'];
518 518
   if (!($TorrentFileName = $Cache->get_value('torrent_file_name_'.$TorrentID))) {
519
-    $DB->query("SELECT File FROM torrents_files WHERE TorrentID=".$TorrentID);
520
-    list($TorrentFile) = $DB->next_record(MYSQLI_NUM, false);
519
+    $TorrentFile = file_get_contents(TORRENT_STORE.$TorrentID.'.torrent');
521 520
     $Tor = new BencodeTorrent($TorrentFile);
522 521
     $TorrentFileName = $Tor->Dec['info']['name'];
523 522
     $Cache->cache_value('torrent_file_name_'.$TorrentID, $TorrentFileName);

+ 1
- 6
sections/torrents/download.php View File

@@ -177,13 +177,8 @@ $DB->query("
177 177
   INSERT IGNORE INTO users_downloads (UserID, TorrentID, Time)
178 178
   VALUES ('$UserID', '$TorrentID', '".sqltime()."')");
179 179
 
180
-$DB->query("
181
-  SELECT File
182
-  FROM torrents_files
183
-  WHERE TorrentID = '$TorrentID'");
184
-
185 180
 Torrents::set_snatch_update_time($UserID, Torrents::SNATCHED_UPDATE_AFTERDL);
186
-list($Contents) = $DB->next_record(MYSQLI_NUM, false);
181
+$Contents = file_get_contents(TORRENT_STORE.$TorrentID.'.torrent');
187 182
 $FileName = TorrentsDL::construct_file_name($Info['PlainArtists'], $Name, $Year, $Media, $Format, $Encoding, $TorrentID, $DownloadAlt);
188 183
 
189 184
 if ($DownloadAlt) {

+ 2
- 14
sections/torrents/redownload.php View File

@@ -76,20 +76,8 @@ $Collector = new TorrentsDL($DownloadsQ, "$Username's ".ucfirst($_GET['type']));
76 76
 while (list($Downloads, $GroupIDs) = $Collector->get_downloads('TorrentID')) {
77 77
   $Artists = Artists::get_artists($GroupIDs);
78 78
   $TorrentIDs = array_keys($GroupIDs);
79
-  $TorrentFilesQ = $DB->query('
80
-    SELECT TorrentID, File
81
-    FROM torrents_files
82
-    WHERE TorrentID IN ('.implode(',', $TorrentIDs).')', false);
83
-  if (is_int($TorrentFilesQ)) {
84
-    // Query failed. Let's not create a broken zip archive
85
-    foreach ($TorrentIDs as $TorrentID) {
86
-      $Download =& $Downloads[$TorrentID];
87
-      $Download['Artist'] = str_replace('–','-',Artists::display_artists($Artists[$Download['GroupID']], false, true, false));
88
-      $Collector->fail_file($Download);
89
-    }
90
-    continue;
91
-  }
92
-  while (list($TorrentID, $TorrentFile) = $DB->next_record(MYSQLI_NUM, false)) {
79
+  foreach ($TorrentIDs as $TorrentID) {
80
+    $TorrentFile = file_get_contents(TORRENT_STORE.$TorrentID.'.torrent');
93 81
     $Download =& $Downloads[$TorrentID];
94 82
     // unzip(1) corrupts files if an emdash is present. Replace them.
95 83
     $Download['Artist'] = str_replace('–','-',Artists::display_artists($Artists[$Download['GroupID']], false, true, false));

+ 2
- 8
sections/upload/generate_extra_torrents.php View File

@@ -48,17 +48,11 @@ foreach ($ExtraTorrents as $ExtraTorrent) {
48 48
     WHERE info_hash = '" . db_string($ThisInsert['InfoHash']) . "'");
49 49
   if ($DB->has_results()) {
50 50
     list($ExtraID) = $DB->next_record();
51
-    $DB->query("
52
-      SELECT TorrentID
53
-      FROM torrents_files
54
-      WHERE TorrentID = $ExtraID");
55
-    if ($DB->has_results()) {
51
+    if (file_exists(TORRENT_STORE.$ExtraID.'.torrent')) {
56 52
       $Err = "<a href=\"torrents.php?torrentid=$ExtraID\">The exact same torrent file already exists on the site!</a>";
57 53
     } else {
58 54
       //One of the lost torrents.
59
-      $DB->query("
60
-        INSERT INTO torrents_files (TorrentID, File)
61
-        VALUES ($ExtraID, '$ThisInsert[TorEnc]')");
55
+      file_put_contents(TORRENT_STORE.$ExtraID.'.torrent', $ThisInsert['TorEnc']);
62 56
       $Err = "<a href=\"torrents.php?torrentid=$ExtraID\">Thank you for fixing this torrent.</a>";
63 57
     }
64 58
   }

+ 1
- 5
sections/upload/insert_extra_torrents.php View File

@@ -27,11 +27,7 @@ foreach ($ExtraTorrentsInsert as $ExtraTorrent) {
27 27
   //******************************************************************************//
28 28
   //--------------- Write torrent file -------------------------------------------//
29 29
 
30
-  $DB->query("
31
-    INSERT INTO torrents_files
32
-      (TorrentID, File)
33
-    VALUES
34
-      ($ExtraTorrentID, '$ExtraTorrent[TorEnc]')");
30
+  file_put_contents(TORRENT_STORE.$ExtraTorrentID.'torrent', $ExtraTorrent['TorEnc']);
35 31
 
36 32
   Misc::write_log("Torrent $ExtraTorrentID ($LogName) (" . number_format($ExtraTorrent['TotalSize'] / (1024 * 1024), 2) . ' MB) was uploaded by ' . $LoggedUser['Username']);
37 33
   Torrents::write_group_log($GroupID, $ExtraTorrentID, $LoggedUser['ID'], 'uploaded (' . number_format($ExtraTorrent['TotalSize'] / (1024 * 1024), 2) . ' MB)', 0);

+ 4
- 12
sections/upload/upload_handle.php View File

@@ -254,7 +254,7 @@ $T['Censored'] = $Properties['Censored'];
254 254
 
255 255
 $Tor = new BencodeTorrent($TorrentName, true);
256 256
 $PublicTorrent = $Tor->make_private(); // The torrent is now private.
257
-$TorEnc = db_string($Tor->encode());
257
+$TorEnc = $Tor->encode();
258 258
 $InfoHash = pack('H*', $Tor->info_hash());
259 259
 
260 260
 $DB->query("
@@ -263,17 +263,11 @@ $DB->query("
263 263
   WHERE info_hash = '".db_string($InfoHash)."'");
264 264
 if ($DB->has_results()) {
265 265
   list($ID) = $DB->next_record();
266
-  $DB->query("
267
-    SELECT TorrentID
268
-    FROM torrents_files
269
-    WHERE TorrentID = $ID");
270
-  if ($DB->has_results()) {
266
+  if (file_exists(TORRENT_STORE.$ID.'.torrent')) {
271 267
     $Err = '<a href="torrents.php?torrentid='.$ID.'">The exact same torrent file already exists on the site!</a>';
272 268
   } else {
273 269
     // A lost torrent
274
-    $DB->query("
275
-      INSERT INTO torrents_files (TorrentID, File)
276
-      VALUES ($ID, '$TorEnc')");
270
+    file_put_contents(TORRENT_STORE.$ID.'.torrent', $TorEnc);
277 271
     $Err = '<a href="torrents.php?torrentid='.$ID.'">Thank you for fixing this torrent</a>';
278 272
   }
279 273
 }
@@ -605,9 +599,7 @@ if ($T['FreeLeechType'] == 3) {
605 599
 //******************************************************************************//
606 600
 //--------------- Write torrent file -------------------------------------------//
607 601
 
608
-$DB->query("
609
-  INSERT INTO torrents_files (TorrentID, File)
610
-  VALUES ($TorrentID, '$TorEnc')");
602
+file_put_contents(TORRENT_STORE.$TorrentID.'.torrent', $TorEnc);
611 603
 Misc::write_log("Torrent $TorrentID ($LogName) (".number_format($TotalSize / (1024 * 1024), 2).' MB) was uploaded by ' . $LoggedUser['Username']);
612 604
 Torrents::write_group_log($GroupID, $TorrentID, $LoggedUser['ID'], 'uploaded ('.number_format($TotalSize / (1024 * 1024), 2).' MB)', 0);
613 605
 

Loading…
Cancel
Save