SingleFileDownloadContext.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef _D_SINGLE_FILE_DOWNLOAD_CONTEXT_H_
  36. #define _D_SINGLE_FILE_DOWNLOAD_CONTEXT_H_
  37. #include "DownloadContext.h"
  38. class SingleFileDownloadContext:public DownloadContext
  39. {
  40. private:
  41. int32_t _pieceLength;
  42. /**
  43. * Actual file path is _dir + _filename.
  44. * If _ufilename is not zero-length string, then _dir + _ufilename.
  45. */
  46. FileEntryHandle _fileEntry;
  47. /**
  48. * _filename and _ufilename may contains directory path name.
  49. * So usr/local/aria2c is acceptable here.
  50. */
  51. string _filename;
  52. string _ufilename;
  53. string _contentType;
  54. Strings _pieceHashes;
  55. string _pieceHashAlgo;
  56. string _checksum;
  57. string _checksumHashAlgo;
  58. void updateFileEntry()
  59. {
  60. if(_ufilename != "") {
  61. _fileEntry->setPath(_ufilename);
  62. } else if(_filename != "") {
  63. _fileEntry->setPath(_filename);
  64. } else {
  65. _fileEntry->setPath("index.html");
  66. }
  67. }
  68. public:
  69. SingleFileDownloadContext(int32_t pieceLength,
  70. int64_t totalLength,
  71. const string& filename,
  72. const string& ufilename = ""):
  73. _pieceLength(pieceLength),
  74. _fileEntry(new FileEntry(filename, totalLength, 0)),
  75. _filename(filename),
  76. _ufilename(ufilename)
  77. {
  78. updateFileEntry();
  79. }
  80. virtual ~SingleFileDownloadContext() {}
  81. virtual string getPieceHash(int32_t index) const
  82. {
  83. if(index < 0 || _pieceHashes.size() <= (size_t)index) {
  84. return "";
  85. }
  86. return _pieceHashes[index];
  87. }
  88. virtual const Strings& getPieceHashes() const
  89. {
  90. return _pieceHashes;
  91. }
  92. virtual int64_t getTotalLength() const
  93. {
  94. return _fileEntry->getLength();
  95. }
  96. virtual FILE_MODE getFileMode() const
  97. {
  98. return SINGLE;
  99. }
  100. virtual FileEntries getFileEntries() const
  101. {
  102. FileEntries fs;
  103. fs.push_back(_fileEntry);
  104. return fs;
  105. }
  106. virtual string getName() const
  107. {
  108. return _filename;
  109. }
  110. virtual int32_t getPieceLength() const
  111. {
  112. return _pieceLength;
  113. }
  114. virtual int32_t getNumPieces() const
  115. {
  116. return (_fileEntry->getLength()+_pieceLength-1)/_pieceLength;
  117. }
  118. virtual string getActualBasePath() const
  119. {
  120. return _dir+"/"+_fileEntry->getPath();
  121. }
  122. virtual string getPieceHashAlgo() const
  123. {
  124. return _pieceHashAlgo;
  125. }
  126. const string& getChecksumHashAlgo() const
  127. {
  128. return _checksumHashAlgo;
  129. }
  130. const string& getChecksum() const
  131. {
  132. return _checksum;
  133. }
  134. void setPieceHashes(const Strings& pieceHashes)
  135. {
  136. _pieceHashes = pieceHashes;
  137. }
  138. void setChecksumHashAlgo(const string& algo)
  139. {
  140. _checksumHashAlgo = algo;
  141. }
  142. void setChecksum(const string& checksum)
  143. {
  144. _checksum = checksum;
  145. }
  146. void setFilename(const string& filename)
  147. {
  148. _filename = filename;
  149. updateFileEntry();
  150. }
  151. void setUFilename(const string& ufilename)
  152. {
  153. _ufilename = ufilename;
  154. updateFileEntry();
  155. }
  156. void setTotalLength(int64_t totalLength)
  157. {
  158. _fileEntry->setLength(totalLength);
  159. }
  160. void setPieceHashAlgo(const string& algo)
  161. {
  162. _pieceHashAlgo = algo;
  163. }
  164. void setContentType(const string& contentType)
  165. {
  166. _contentType = contentType;
  167. }
  168. const string& getContentType()
  169. {
  170. return _contentType;
  171. }
  172. };
  173. typedef SharedHandle<SingleFileDownloadContext> SingleFileDownloadContextHandle;
  174. #endif // _D_SINGLE_FILE_DOWNLOAD_CONTEXT_H_