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