Browse Source

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

spaghetti 8 years ago
parent
commit
ba83225b19

+ 4
- 3
classes/config.template View File

5
 //date_default_timezone_set('EST');
5
 //date_default_timezone_set('EST');
6
 
6
 
7
 // Main settings
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
 // Tracker urls to be added to torrent files ala bittorrent.org/beps/bep_0012.html
13
 // Tracker urls to be added to torrent files ala bittorrent.org/beps/bep_0012.html
13
 define('ANNOUNCE_URLS', [[
14
 define('ANNOUNCE_URLS', [[

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

324
       G::$Cache->decrement('num_torrent_reportsv2', $Reports);
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
     G::$DB->query("
328
     G::$DB->query("
331
       DELETE FROM torrents_bad_tags
329
       DELETE FROM torrents_bad_tags
332
       WHERE TorrentID = $ID");
330
       WHERE TorrentID = $ID");
567
     $QueryID = G::$DB->get_query_id();
565
     $QueryID = G::$DB->get_query_id();
568
 
566
 
569
     G::$DB->query("
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
     if (G::$DB->has_results()) {
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
       if (Misc::is_new_torrent($Contents)) {
574
       if (Misc::is_new_torrent($Contents)) {
579
         $Tor = new BencodeTorrent($Contents);
575
         $Tor = new BencodeTorrent($Contents);
580
         $FilePath = (isset($Tor->Dec['info']['files']) ? Format::make_utf8($Tor->get_name()) : '');
576
         $FilePath = (isset($Tor->Dec['info']['files']) ? Format::make_utf8($Tor->get_name()) : '');

+ 0
- 6
gazelle.sql View File

1181
   KEY `TimeAdded` (`TimeAdded`)
1181
   KEY `TimeAdded` (`TimeAdded`)
1182
 ) ENGINE=InnoDB CHARSET=utf8;
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
 CREATE TABLE `torrents_group` (
1184
 CREATE TABLE `torrents_group` (
1191
   `ID` int(10) NOT NULL AUTO_INCREMENT,
1185
   `ID` int(10) NOT NULL AUTO_INCREMENT,
1192
   `CategoryID` int(10) DEFAULT NULL,
1186
   `CategoryID` int(10) DEFAULT NULL,

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

44
 FROM torrents AS t
44
 FROM torrents AS t
45
   INNER JOIN torrents_group AS tg ON tg.ID = t.GroupID AND tg.CategoryID = '1'
45
   INNER JOIN torrents_group AS tg ON tg.ID = t.GroupID AND tg.CategoryID = '1'
46
   INNER JOIN artists_group AS a ON a.ArtistID = tg.ArtistID AND a.ArtistID = '59721'
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
 ORDER BY t.GroupID ASC, Rank DESC, t.Seeders ASC
47
 ORDER BY t.GroupID ASC, Rank DESC, t.Seeders ASC
49
 */
48
 */
50
 
49
 
142
 $Collector = new TorrentsDL($DownloadsQ, $ArtistName);
141
 $Collector = new TorrentsDL($DownloadsQ, $ArtistName);
143
 while (list($Downloads, $GroupIDs) = $Collector->get_downloads('GroupID')) {
142
 while (list($Downloads, $GroupIDs) = $Collector->get_downloads('GroupID')) {
144
   $Artists = Artists::get_artists($GroupIDs);
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
     $GroupID = $GroupIDs[$TorrentID];
147
     $GroupID = $GroupIDs[$TorrentID];
160
     $Download =& $Downloads[$GroupID];
148
     $Download =& $Downloads[$GroupID];
161
     $Download['Artist'] = Artists::display_artists($Artists[$Download['GroupID']], false, true, false);
149
     $Download['Artist'] = Artists::display_artists($Artists[$Download['GroupID']], false, true, false);

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

44
   INNER JOIN collages_torrents AS c ON t.GroupID = c.GroupID AND c.CollageID = '8'
44
   INNER JOIN collages_torrents AS c ON t.GroupID = c.GroupID AND c.CollageID = '8'
45
   INNER JOIN torrents_group AS tg ON tg.ID = t.GroupID AND tg.CategoryID = '1'
45
   INNER JOIN torrents_group AS tg ON tg.ID = t.GroupID AND tg.CategoryID = '1'
46
   LEFT JOIN artists_group AS a ON a.ArtistID = tg.ArtistID
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
 ORDER BY t.GroupID ASC, Rank DESC, t.Seeders ASC
47
 ORDER BY t.GroupID ASC, Rank DESC, t.Seeders ASC
49
 */
48
 */
50
 
49
 
133
 
132
 
134
 while (list($Downloads, $GroupIDs) = $Collector->get_downloads('GroupID')) {
133
 while (list($Downloads, $GroupIDs) = $Collector->get_downloads('GroupID')) {
135
   $Artists = Artists::get_artists($GroupIDs);
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
     $GroupID = $GroupIDs[$TorrentID];
138
     $GroupID = $GroupIDs[$TorrentID];
151
     $Download =& $Downloads[$GroupID];
139
     $Download =& $Downloads[$GroupID];
152
     $Download['Artist'] = Artists::display_artists($Artists[$Download['GroupID']], false, true, false);
140
     $Download['Artist'] = Artists::display_artists($Artists[$Download['GroupID']], false, true, false);

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

582
       $SnatchedTorrentClass = $Data['IsSnatched'] ? ' snatched_torrent' : '';
582
       $SnatchedTorrentClass = $Data['IsSnatched'] ? ' snatched_torrent' : '';
583
       $TorrentDL = "torrents.php?action=download&id=".$TorrentID."&authkey=".$LoggedUser['AuthKey']."&torrent_pass=".$LoggedUser['torrent_pass'];
583
       $TorrentDL = "torrents.php?action=download&id=".$TorrentID."&authkey=".$LoggedUser['AuthKey']."&torrent_pass=".$LoggedUser['torrent_pass'];
584
       if (!($TorrentFileName = $Cache->get_value('torrent_file_name_'.$TorrentID))) {
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
         $Tor = new BencodeTorrent($TorrentFile);
586
         $Tor = new BencodeTorrent($TorrentFile);
588
         $TorrentFileName = $Tor->Dec['info']['name'];
587
         $TorrentFileName = $Tor->Dec['info']['name'];
589
         $Cache->cache_value('torrent_file_name_'.$TorrentID, $TorrentFileName);
588
         $Cache->cache_value('torrent_file_name_'.$TorrentID, $TorrentFileName);
655
     $SnatchedTorrentClass = $Data['IsSnatched'] ? ' snatched_torrent' : '';
654
     $SnatchedTorrentClass = $Data['IsSnatched'] ? ' snatched_torrent' : '';
656
     $TorrentDL = "torrents.php?action=download&id=".$TorrentID."&authkey=".$LoggedUser['AuthKey']."&torrent_pass=".$LoggedUser['torrent_pass'];
655
     $TorrentDL = "torrents.php?action=download&id=".$TorrentID."&authkey=".$LoggedUser['AuthKey']."&torrent_pass=".$LoggedUser['torrent_pass'];
657
     if (!($TorrentFileName = $Cache->get_value('torrent_file_name_'.$TorrentID))) {
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
       $Tor = new BencodeTorrent($TorrentFile);
658
       $Tor = new BencodeTorrent($TorrentFile);
661
       $TorrentFileName = $Tor->Dec['info']['name'];
659
       $TorrentFileName = $Tor->Dec['info']['name'];
662
       $Cache->cache_value('torrent_file_name_'.$TorrentID, $TorrentFileName);
660
       $Cache->cache_value('torrent_file_name_'.$TorrentID, $TorrentFileName);

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

516
 
516
 
517
   $TorrentDL = "torrents.php?action=download&id=".$TorrentID."&authkey=".$LoggedUser['AuthKey']."&torrent_pass=".$LoggedUser['torrent_pass'];
517
   $TorrentDL = "torrents.php?action=download&id=".$TorrentID."&authkey=".$LoggedUser['AuthKey']."&torrent_pass=".$LoggedUser['torrent_pass'];
518
   if (!($TorrentFileName = $Cache->get_value('torrent_file_name_'.$TorrentID))) {
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
     $Tor = new BencodeTorrent($TorrentFile);
520
     $Tor = new BencodeTorrent($TorrentFile);
522
     $TorrentFileName = $Tor->Dec['info']['name'];
521
     $TorrentFileName = $Tor->Dec['info']['name'];
523
     $Cache->cache_value('torrent_file_name_'.$TorrentID, $TorrentFileName);
522
     $Cache->cache_value('torrent_file_name_'.$TorrentID, $TorrentFileName);

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

177
   INSERT IGNORE INTO users_downloads (UserID, TorrentID, Time)
177
   INSERT IGNORE INTO users_downloads (UserID, TorrentID, Time)
178
   VALUES ('$UserID', '$TorrentID', '".sqltime()."')");
178
   VALUES ('$UserID', '$TorrentID', '".sqltime()."')");
179
 
179
 
180
-$DB->query("
181
-  SELECT File
182
-  FROM torrents_files
183
-  WHERE TorrentID = '$TorrentID'");
184
-
185
 Torrents::set_snatch_update_time($UserID, Torrents::SNATCHED_UPDATE_AFTERDL);
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
 $FileName = TorrentsDL::construct_file_name($Info['PlainArtists'], $Name, $Year, $Media, $Format, $Encoding, $TorrentID, $DownloadAlt);
182
 $FileName = TorrentsDL::construct_file_name($Info['PlainArtists'], $Name, $Year, $Media, $Format, $Encoding, $TorrentID, $DownloadAlt);
188
 
183
 
189
 if ($DownloadAlt) {
184
 if ($DownloadAlt) {

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

76
 while (list($Downloads, $GroupIDs) = $Collector->get_downloads('TorrentID')) {
76
 while (list($Downloads, $GroupIDs) = $Collector->get_downloads('TorrentID')) {
77
   $Artists = Artists::get_artists($GroupIDs);
77
   $Artists = Artists::get_artists($GroupIDs);
78
   $TorrentIDs = array_keys($GroupIDs);
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
     $Download =& $Downloads[$TorrentID];
81
     $Download =& $Downloads[$TorrentID];
94
     // unzip(1) corrupts files if an emdash is present. Replace them.
82
     // unzip(1) corrupts files if an emdash is present. Replace them.
95
     $Download['Artist'] = str_replace('–','-',Artists::display_artists($Artists[$Download['GroupID']], false, true, false));
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
     WHERE info_hash = '" . db_string($ThisInsert['InfoHash']) . "'");
48
     WHERE info_hash = '" . db_string($ThisInsert['InfoHash']) . "'");
49
   if ($DB->has_results()) {
49
   if ($DB->has_results()) {
50
     list($ExtraID) = $DB->next_record();
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
       $Err = "<a href=\"torrents.php?torrentid=$ExtraID\">The exact same torrent file already exists on the site!</a>";
52
       $Err = "<a href=\"torrents.php?torrentid=$ExtraID\">The exact same torrent file already exists on the site!</a>";
57
     } else {
53
     } else {
58
       //One of the lost torrents.
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
       $Err = "<a href=\"torrents.php?torrentid=$ExtraID\">Thank you for fixing this torrent.</a>";
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
   //******************************************************************************//
27
   //******************************************************************************//
28
   //--------------- Write torrent file -------------------------------------------//
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
   Misc::write_log("Torrent $ExtraTorrentID ($LogName) (" . number_format($ExtraTorrent['TotalSize'] / (1024 * 1024), 2) . ' MB) was uploaded by ' . $LoggedUser['Username']);
32
   Misc::write_log("Torrent $ExtraTorrentID ($LogName) (" . number_format($ExtraTorrent['TotalSize'] / (1024 * 1024), 2) . ' MB) was uploaded by ' . $LoggedUser['Username']);
37
   Torrents::write_group_log($GroupID, $ExtraTorrentID, $LoggedUser['ID'], 'uploaded (' . number_format($ExtraTorrent['TotalSize'] / (1024 * 1024), 2) . ' MB)', 0);
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
 
254
 
255
 $Tor = new BencodeTorrent($TorrentName, true);
255
 $Tor = new BencodeTorrent($TorrentName, true);
256
 $PublicTorrent = $Tor->make_private(); // The torrent is now private.
256
 $PublicTorrent = $Tor->make_private(); // The torrent is now private.
257
-$TorEnc = db_string($Tor->encode());
257
+$TorEnc = $Tor->encode();
258
 $InfoHash = pack('H*', $Tor->info_hash());
258
 $InfoHash = pack('H*', $Tor->info_hash());
259
 
259
 
260
 $DB->query("
260
 $DB->query("
263
   WHERE info_hash = '".db_string($InfoHash)."'");
263
   WHERE info_hash = '".db_string($InfoHash)."'");
264
 if ($DB->has_results()) {
264
 if ($DB->has_results()) {
265
   list($ID) = $DB->next_record();
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
     $Err = '<a href="torrents.php?torrentid='.$ID.'">The exact same torrent file already exists on the site!</a>';
267
     $Err = '<a href="torrents.php?torrentid='.$ID.'">The exact same torrent file already exists on the site!</a>';
272
   } else {
268
   } else {
273
     // A lost torrent
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
     $Err = '<a href="torrents.php?torrentid='.$ID.'">Thank you for fixing this torrent</a>';
271
     $Err = '<a href="torrents.php?torrentid='.$ID.'">Thank you for fixing this torrent</a>';
278
   }
272
   }
279
 }
273
 }
605
 //******************************************************************************//
599
 //******************************************************************************//
606
 //--------------- Write torrent file -------------------------------------------//
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
 Misc::write_log("Torrent $TorrentID ($LogName) (".number_format($TotalSize / (1024 * 1024), 2).' MB) was uploaded by ' . $LoggedUser['Username']);
603
 Misc::write_log("Torrent $TorrentID ($LogName) (".number_format($TotalSize / (1024 * 1024), 2).' MB) was uploaded by ' . $LoggedUser['Username']);
612
 Torrents::write_group_log($GroupID, $TorrentID, $LoggedUser['ID'], 'uploaded ('.number_format($TotalSize / (1024 * 1024), 2).' MB)', 0);
604
 Torrents::write_group_log($GroupID, $TorrentID, $LoggedUser['ID'], 'uploaded ('.number_format($TotalSize / (1024 * 1024), 2).' MB)', 0);
613
 
605
 

Loading…
Cancel
Save