<commit_before>/* 
 * Copyright 2014 OpenMarket Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.matrix.androidsdk.rest.model;

import android.net.Uri;

import java.io.File;

public class FileMessage extends Message {
    public FileInfo info;
    public String url;

    public FileMessage() {
        msgtype = MSGTYPE_FILE;
    }

    public boolean isLocalContent() {
        return (null != url) && (url.startsWith("file://"));
    }

    public String getMimeType() {
        if (null != info) {
            return info.mimetype;
        } else {
            return null;
        }
    }

    /**
     * Checks if the media Urls are still valid.
     * The media Urls could define a file path.
     * They could have been deleted after a media cache cleaning.
     */
    public void checkMediaUrls() {
        if ((url != null) && url.startsWith("file://")) {
            try {<commit_msg>Try to retrieve the mimetype when it is defined (from the filename)<commit_after>/* 
 * Copyright 2014 OpenMarket Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.matrix.androidsdk.rest.model;

import android.net.Uri;
import android.text.TextUtils;
import android.webkit.MimeTypeMap;

import java.io.File;

public class FileMessage extends Message {
    public FileInfo info;
    public String url;

    public FileMessage() {
        msgtype = MSGTYPE_FILE;
    }

    public boolean isLocalContent() {
        return (null != url) && (url.startsWith("file://"));
    }

    public String getMimeType() {
        if (null != info) {
            // the mimetype was not provided
            if (TextUtils.isEmpty(info.mimetype) && (body.indexOf('.') > 0)) {
                // the body should contain the filename so try to extract the mimetype from the extension
                String extension =  body.substring(body.lastIndexOf('.') + 1, body.length());

                try {
                    info.mimetype = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
                } catch (Exception e) {

                }
            }

            return info.mimetype;
        } else {
            return null;
        }
    }

    /**
     * Checks if the media Urls are still valid.
     * The media Urls could define a file path.
     * They could have been deleted after a media cache cleaning.
     */
    public void checkMediaUrls() {
        if ((url != null) && url.startsWith("file://")) {
            try {
{'fraction_non_alphanumeric': 0.09322276236774378, 'fraction_numerical': 0.005147269087789534, 'mean_word_length': 2.412682926829268, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2771706', 'n_tokens_mistral': 959, 'n_tokens_neox': 916, 'n_words': 362}