oops
[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.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 *md)
39 {
40   static const char *mimeMap[][2] = {
41     {"application/bz2", ".bz2"},
42     {"application/gnunet-directory", ".gnd"},
43     {"application/java", ".class"},
44     {"application/msword", ".doc"},
45     {"application/ogg", ".ogg"},
46     {"application/pdf", ".pdf"},
47     {"application/pgp-keys", ".key"},
48     {"application/pgp-signature", ".pgp"},
49     {"application/postscript", ".ps"},
50     {"application/rar", ".rar"},
51     {"application/rtf", ".rtf"},
52     {"application/xml", ".xml"},
53     {"application/x-debian-package", ".deb"},
54     {"application/x-dvi", ".dvi"},
55     {"applixation/x-flac", ".flac"},
56     {"applixation/x-gzip", ".gz"},
57     {"application/x-java-archive", ".jar"},
58     {"application/x-java-vm", ".class"},
59     {"application/x-python-code", ".pyc"},
60     {"application/x-redhat-package-manager", ".rpm"},
61     {"application/x-rpm", ".rpm"},
62     {"application/x-tar", ".tar"},
63     {"application/x-tex-pk", ".pk"},
64     {"application/x-texinfo", ".texinfo"},
65     {"application/x-xcf", ".xcf"},
66     {"application/x-xfig", ".xfig"},
67     {"application/zip", ".zip"},
68     
69     {"audio/midi", ".midi"},
70     {"audio/mpeg", ".mp3"},
71     {"audio/real", ".rm"},
72     {"audio/x-wav", ".wav"},
73     
74     {"image/gif", ".gif"},
75     {"image/jpeg", ".jpg"},
76     {"image/pcx", ".pcx"},
77     {"image/png", ".png"},
78     {"image/tiff", ".tiff"},
79     {"image/x-ms-bmp", ".bmp"},
80     {"image/x-xpixmap", ".xpm"},
81     
82     {"text/css", ".css"},
83     {"text/html", ".html"},
84     {"text/plain", ".txt"},
85     {"text/rtf", ".rtf"},
86     {"text/x-c++hdr", ".h++"},
87     {"text/x-c++src", ".c++"},
88     {"text/x-chdr", ".h"},
89     {"text/x-csrc", ".c"},
90     {"text/x-java", ".java"},
91     {"text/x-moc", ".moc"},
92     {"text/x-pascal", ".pas"},
93     {"text/x-perl", ".pl"},
94     {"text/x-python", ".py"},
95     {"text/x-tex", ".tex"},
96     
97     {"video/avi", ".avi"},
98     {"video/mpeg", ".mpeg"},
99     {"video/quicktime", ".qt"},
100     {"video/real", ".rm"},
101     {"video/x-msvideo", ".avi"},
102     {NULL, NULL},
103   };
104   char *ret;
105   unsigned int i;
106   char *mime;
107   char *base;
108   const char *ext;
109
110   ret = GNUNET_CONTAINER_meta_data_get_by_type (md,
111                                                 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
112   if (ret != NULL)
113     return ret;  
114   ext = NULL;
115   mime = GNUNET_CONTAINER_meta_data_get_by_type (md,
116                                                  EXTRACTOR_METATYPE_MIMETYPE);
117   if (mime != NULL)
118     {
119       i = 0;
120       while ( (mimeMap[i][0] != NULL) && 
121               (0 != strcmp (mime, mimeMap[i][0])))
122         i++;
123       if (mimeMap[i][1] == NULL)
124         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | 
125                     GNUNET_ERROR_TYPE_BULK,
126                     _("Did not find mime type `%s' in extension list.\n"),
127                     mime);
128       else
129         ext = mimeMap[i][1];
130       GNUNET_free (mime);
131     }
132   base = 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) &&
151        (ext == NULL) )
152     return NULL;
153   if (base == NULL)
154     return GNUNET_strdup (ext);
155   if (ext == NULL)
156     return base;
157   GNUNET_asprintf (&ret,
158                    "%s%s",
159                    base,
160                    ext);
161   GNUNET_free (base);
162   return ret;
163 }
164
165 /* end of fs_misc.c */