fs_misc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010, 2011, 2017 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file fs/fs_misc.c
  18. * @brief misc. functions related to file-sharing in general
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_constants.h"
  23. #include "gnunet_fs_service.h"
  24. #include "fs_api.h"
  25. /**
  26. * Suggest a filename based on given metadata.
  27. *
  28. * @param md given meta data
  29. * @return NULL if meta data is useless for suggesting a filename
  30. */
  31. char *
  32. GNUNET_FS_meta_data_suggest_filename (const struct GNUNET_CONTAINER_MetaData
  33. *md)
  34. {
  35. static const char *mimeMap[][2] = {
  36. { "application/bz2", ".bz2" },
  37. { "application/gnunet-directory", ".gnd" },
  38. { "application/java", ".class" },
  39. { "application/msword", ".doc" },
  40. { "application/nar", ".nar" },
  41. { "application/narinfo", ".narinfo" },
  42. { "application/ogg", ".ogg" },
  43. { "application/pdf", ".pdf" },
  44. { "application/pgp-keys", ".key" },
  45. { "application/pgp-signature", ".pgp" },
  46. { "application/postscript", ".ps" },
  47. { "application/rar", ".rar" },
  48. { "application/rtf", ".rtf" },
  49. { "application/xml", ".xml" },
  50. { "application/x-debian-package", ".deb" },
  51. { "application/x-dvi", ".dvi" },
  52. { "application/x-flac", ".flac" },
  53. { "application/x-gzip", ".gz" },
  54. { "application/x-java-archive", ".jar" },
  55. { "application/x-java-vm", ".class" },
  56. { "application/x-python-code", ".pyc" },
  57. { "application/x-redhat-package-manager", ".rpm" },
  58. { "application/x-rpm", ".rpm" },
  59. { "application/x-tar", ".tar" },
  60. { "application/x-tex-pk", ".pk" },
  61. { "application/x-texinfo", ".texinfo" },
  62. { "application/x-xcf", ".xcf" },
  63. { "application/x-xfig", ".xfig" },
  64. { "application/zip", ".zip" },
  65. { "audio/midi", ".midi" },
  66. { "audio/mpeg", ".mp3" },
  67. { "audio/real", ".rm" },
  68. { "audio/x-wav", ".wav" },
  69. { "image/gif", ".gif" },
  70. { "image/jpeg", ".jpg" },
  71. { "image/pcx", ".pcx" },
  72. { "image/png", ".png" },
  73. { "image/tiff", ".tiff" },
  74. { "image/x-ms-bmp", ".bmp" },
  75. { "image/x-xpixmap", ".xpm" },
  76. { "text/css", ".css" },
  77. { "text/html", ".html" },
  78. { "text/plain", ".txt" },
  79. { "text/rtf", ".rtf" },
  80. { "text/x-c++hdr", ".h++" },
  81. { "text/x-c++src", ".c++" },
  82. { "text/x-chdr", ".h" },
  83. { "text/x-csrc", ".c" },
  84. { "text/x-java", ".java" },
  85. { "text/x-moc", ".moc" },
  86. { "text/x-pascal", ".pas" },
  87. { "text/x-perl", ".pl" },
  88. { "text/x-python", ".py" },
  89. { "text/x-tex", ".tex" },
  90. { "video/avi", ".avi" },
  91. { "video/mpeg", ".mpeg" },
  92. { "video/quicktime", ".qt" },
  93. { "video/real", ".rm" },
  94. { "video/x-msvideo", ".avi" },
  95. { NULL, NULL },
  96. };
  97. char *ret;
  98. unsigned int i;
  99. char *mime;
  100. char *base;
  101. const char *ext;
  102. ret =
  103. GNUNET_CONTAINER_meta_data_get_by_type (md,
  104. EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
  105. if (ret != NULL)
  106. return ret;
  107. ext = NULL;
  108. mime =
  109. GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_METATYPE_MIMETYPE);
  110. if (mime != NULL)
  111. {
  112. i = 0;
  113. while ((mimeMap[i][0] != NULL) && (0 != strcmp (mime, mimeMap[i][0])))
  114. i++;
  115. if (mimeMap[i][1] == NULL)
  116. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
  117. _ ("Did not find mime type `%s' in extension list.\n"), mime);
  118. else
  119. ext = mimeMap[i][1];
  120. GNUNET_free (mime);
  121. }
  122. base =
  123. GNUNET_CONTAINER_meta_data_get_first_by_types (md,
  124. EXTRACTOR_METATYPE_TITLE,
  125. EXTRACTOR_METATYPE_BOOK_TITLE,
  126. EXTRACTOR_METATYPE_ORIGINAL_TITLE,
  127. EXTRACTOR_METATYPE_PACKAGE_NAME,
  128. EXTRACTOR_METATYPE_URL,
  129. EXTRACTOR_METATYPE_URI,
  130. EXTRACTOR_METATYPE_DESCRIPTION,
  131. EXTRACTOR_METATYPE_ISRC,
  132. EXTRACTOR_METATYPE_JOURNAL_NAME,
  133. EXTRACTOR_METATYPE_AUTHOR_NAME,
  134. EXTRACTOR_METATYPE_SUBJECT,
  135. EXTRACTOR_METATYPE_ALBUM,
  136. EXTRACTOR_METATYPE_ARTIST,
  137. EXTRACTOR_METATYPE_KEYWORDS,
  138. EXTRACTOR_METATYPE_COMMENT,
  139. EXTRACTOR_METATYPE_UNKNOWN,
  140. -1);
  141. if ((base == NULL) && (ext == NULL))
  142. return NULL;
  143. if (base == NULL)
  144. return GNUNET_strdup (ext);
  145. if (ext == NULL)
  146. return base;
  147. GNUNET_asprintf (&ret, "%s%s", base, ext);
  148. GNUNET_free (base);
  149. return ret;
  150. }
  151. /* end of fs_misc.c */