error handling
[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 Affero 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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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/nar", ".nar" },
47     { "application/narinfo", ".narinfo" },
48     { "application/ogg", ".ogg" },
49     { "application/pdf", ".pdf" },
50     { "application/pgp-keys", ".key" },
51     { "application/pgp-signature", ".pgp" },
52     { "application/postscript", ".ps" },
53     { "application/rar", ".rar" },
54     { "application/rtf", ".rtf" },
55     { "application/xml", ".xml" },
56     { "application/x-debian-package", ".deb" },
57     { "application/x-dvi", ".dvi" },
58     { "application/x-flac", ".flac" },
59     { "application/x-gzip", ".gz" },
60     { "application/x-java-archive", ".jar" },
61     { "application/x-java-vm", ".class" },
62     { "application/x-python-code", ".pyc" },
63     { "application/x-redhat-package-manager", ".rpm" },
64     { "application/x-rpm", ".rpm" },
65     { "application/x-tar", ".tar" },
66     { "application/x-tex-pk", ".pk" },
67     { "application/x-texinfo", ".texinfo" },
68     { "application/x-xcf", ".xcf" },
69     { "application/x-xfig", ".xfig" },
70     { "application/zip", ".zip" },
71
72     { "audio/midi", ".midi" },
73     { "audio/mpeg", ".mp3" },
74     { "audio/real", ".rm" },
75     { "audio/x-wav", ".wav" },
76
77     { "image/gif", ".gif" },
78     { "image/jpeg", ".jpg" },
79     { "image/pcx", ".pcx" },
80     { "image/png", ".png" },
81     { "image/tiff", ".tiff" },
82     { "image/x-ms-bmp", ".bmp" },
83     { "image/x-xpixmap", ".xpm" },
84
85     { "text/css", ".css" },
86     { "text/html", ".html" },
87     { "text/plain", ".txt" },
88     { "text/rtf", ".rtf" },
89     { "text/x-c++hdr", ".h++" },
90     { "text/x-c++src", ".c++" },
91     { "text/x-chdr", ".h" },
92     { "text/x-csrc", ".c" },
93     { "text/x-java", ".java" },
94     { "text/x-moc", ".moc" },
95     { "text/x-pascal", ".pas" },
96     { "text/x-perl", ".pl" },
97     { "text/x-python", ".py" },
98     { "text/x-tex", ".tex" },
99
100     { "video/avi", ".avi" },
101     { "video/mpeg", ".mpeg" },
102     { "video/quicktime", ".qt" },
103     { "video/real", ".rm" },
104     { "video/x-msvideo", ".avi" },
105     { NULL, NULL },
106   };
107   char *ret;
108   unsigned int i;
109   char *mime;
110   char *base;
111   const char *ext;
112
113   ret =
114     GNUNET_CONTAINER_meta_data_get_by_type (md,
115                                             EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
116   if (ret != NULL)
117     return ret;
118   ext = NULL;
119   mime =
120     GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_METATYPE_MIMETYPE);
121   if (mime != NULL)
122   {
123     i = 0;
124     while ((mimeMap[i][0] != NULL) && (0 != strcmp (mime, mimeMap[i][0])))
125       i++;
126     if (mimeMap[i][1] == NULL)
127       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
128                   _ ("Did not find mime type `%s' in extension list.\n"), mime);
129     else
130       ext = mimeMap[i][1];
131     GNUNET_free (mime);
132   }
133   base =
134     GNUNET_CONTAINER_meta_data_get_first_by_types (md,
135                                                    EXTRACTOR_METATYPE_TITLE,
136                                                    EXTRACTOR_METATYPE_BOOK_TITLE,
137                                                    EXTRACTOR_METATYPE_ORIGINAL_TITLE,
138                                                    EXTRACTOR_METATYPE_PACKAGE_NAME,
139                                                    EXTRACTOR_METATYPE_URL,
140                                                    EXTRACTOR_METATYPE_URI,
141                                                    EXTRACTOR_METATYPE_DESCRIPTION,
142                                                    EXTRACTOR_METATYPE_ISRC,
143                                                    EXTRACTOR_METATYPE_JOURNAL_NAME,
144                                                    EXTRACTOR_METATYPE_AUTHOR_NAME,
145                                                    EXTRACTOR_METATYPE_SUBJECT,
146                                                    EXTRACTOR_METATYPE_ALBUM,
147                                                    EXTRACTOR_METATYPE_ARTIST,
148                                                    EXTRACTOR_METATYPE_KEYWORDS,
149                                                    EXTRACTOR_METATYPE_COMMENT,
150                                                    EXTRACTOR_METATYPE_UNKNOWN,
151                                                    -1);
152   if ((base == NULL) && (ext == NULL))
153     return NULL;
154   if (base == NULL)
155     return GNUNET_strdup (ext);
156   if (ext == NULL)
157     return base;
158   GNUNET_asprintf (&ret, "%s%s", base, ext);
159   GNUNET_free (base);
160   return ret;
161 }
162
163
164 /* end of fs_misc.c */