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