{"image", MIMETYPE_IMAGE},
{"audio", MIMETYPE_AUDIO},
{"video", MIMETYPE_VIDEO},
+ {"model", MIMETYPE_MODEL},
{"application", MIMETYPE_APPLICATION},
{"message", MIMETYPE_MESSAGE},
{"multipart", MIMETYPE_MULTIPART},
return base;
}
+void *procmime_get_part_as_string(MimeInfo *mimeinfo,
+ gboolean null_terminate)
+{
+ FILE *infp;
+ gchar *data;
+ gint length, readlength;
+
+ cm_return_val_if_fail(mimeinfo != NULL, NULL);
+
+ if (mimeinfo->encoding_type != ENC_BINARY &&
+ !procmime_decode_content(mimeinfo))
+ return NULL;
+
+ if (mimeinfo->content == MIMECONTENT_MEM)
+ return g_strdup(mimeinfo->data.mem);
+
+ if ((infp = claws_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+ FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
+ return NULL;
+ }
+
+ if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
+ FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
+ claws_fclose(infp);
+ return NULL;
+ }
+
+ length = mimeinfo->length;
+
+ data = g_malloc(null_terminate ? length + 1 : length);
+ if (data == NULL) {
+ g_warning("Could not allocate %d bytes for procmime_get_part_as_string.\n",
+ (null_terminate ? length + 1 : length));
+ claws_fclose(infp);
+ return NULL;
+ }
+
+ readlength = claws_fread(data, length, 1, infp);
+ if (readlength <= 0) {
+ FILE_OP_ERROR(mimeinfo->data.filename, "fread");
+ g_free(data);
+ claws_fclose(infp);
+ return NULL;
+ }
+
+ claws_fclose(infp);
+
+ if (null_terminate)
+ data[length] = '\0';
+
+ return data;
+}
+
+/* Returns an open GInputStream. The caller should just
+ * read mimeinfo->length bytes from it and then release it. */
+GInputStream *procmime_get_part_as_inputstream(MimeInfo *mimeinfo,
+ GError **error)
+{
+ cm_return_val_if_fail(mimeinfo != NULL, NULL);
+
+ if (mimeinfo->encoding_type != ENC_BINARY &&
+ !procmime_decode_content(mimeinfo))
+ return NULL;
+
+ if (mimeinfo->content == MIMECONTENT_MEM) {
+ /* NULL for destroy func, since we're not copying
+ * the data for the stream. */
+ return g_memory_input_stream_new_from_data(
+ mimeinfo->data.mem,
+ mimeinfo->length, NULL);
+ } else {
+ return g_memory_input_stream_new_from_data(
+ procmime_get_part_as_string(mimeinfo, FALSE),
+ mimeinfo->length, g_free);
+ }
+}