Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8e64aa9c authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "OPP: Truncating receiving filename"

parents 3649d3f4 6dfb24a2
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import android.content.ContentResolver;
import android.content.ContentValues;
@@ -57,6 +59,10 @@ public class BluetoothOppReceiveFileInfo {
    private static final boolean V = Constants.VERBOSE;
    private static String sDesiredStoragePath = null;

    /* To truncate the name of the received file if the length exceeds 245 */
    private static final int OPP_LENGTH_OF_FILE_NAME = 244;


    /** absolute store file name */
    public String mFileName;

@@ -153,6 +159,30 @@ public class BluetoothOppReceiveFileInfo {
            extension = filename.substring(dotIndex);
            filename = filename.substring(0, dotIndex);
        }
        if (D) Log.d(Constants.TAG, " File Name " + filename);

        if (filename.getBytes().length > OPP_LENGTH_OF_FILE_NAME) {
          /* Including extn of the file, Linux supports 255 character as a maximum length of the
           * file name to be created. Hence, Instead of sending OBEX_HTTP_INTERNAL_ERROR,
           * as a response, truncate the length of the file name and save it. This check majorly
           * helps in the case of vcard, where Phone book app supports contact name to be saved
           * more than 255 characters, But the server rejects the card just because the length of
           * vcf file name received exceeds 255 Characters.
           */
              Log.i(Constants.TAG, " File Name Length :" + filename.length());
              Log.i(Constants.TAG, " File Name Length in Bytes:" + filename.getBytes().length);

          try {
              byte[] oldfilename = filename.getBytes("UTF-8");
              byte[] newfilename = new byte[OPP_LENGTH_OF_FILE_NAME];
              System.arraycopy(oldfilename, 0, newfilename, 0, OPP_LENGTH_OF_FILE_NAME);
              filename = new String(newfilename, "UTF-8");
          } catch (UnsupportedEncodingException e) {
              Log.e(Constants.TAG, "Exception: " + e);
          }
          if (D) Log.d(Constants.TAG, "File name is too long. Name is truncated as: " + filename);
        }

        filename = base.getPath() + File.separator + filename;
        // Generate a unique filename, create the file, return it.
        String fullfilename = chooseUniquefilename(filename, extension);