13
13
import android .webkit .MimeTypeMap ;
14
14
import android .widget .Toast ;
15
15
16
+ import com .j256 .simplemagic .ContentInfo ;
17
+ import com .j256 .simplemagic .ContentInfoUtil ;
16
18
import java .io .File ;
17
19
import java .io .FileInputStream ;
18
20
import java .io .FileNotFoundException ;
21
+ import java .io .FilterInputStream ;
19
22
import java .io .IOException ;
20
23
import java .io .InputStream ;
21
24
import java .util .HashSet ;
@@ -68,6 +71,7 @@ public class UploadService extends HandlerService<Contribution> {
68
71
public static final int NOTIFICATION_UPLOAD_IN_PROGRESS = 1 ;
69
72
public static final int NOTIFICATION_UPLOAD_COMPLETE = 2 ;
70
73
public static final int NOTIFICATION_UPLOAD_FAILED = 3 ;
74
+ private ContentInfoUtil contentInfoUtil ;
71
75
72
76
public UploadService () {
73
77
super ("UploadService" );
@@ -129,7 +133,6 @@ public void onCreate() {
129
133
protected void handle (int what , Contribution contribution ) {
130
134
switch (what ) {
131
135
case ACTION_UPLOAD_FILE :
132
- //FIXME: Google Photos bug
133
136
uploadContribution (contribution );
134
137
break ;
135
138
default :
@@ -195,20 +198,34 @@ private NotificationCompat.Builder getNotificationBuilder(Contribution contribut
195
198
}
196
199
197
200
private void uploadContribution (Contribution contribution ) {
198
- InputStream fileInputStream ;
199
-
201
+ InputStream fileInputStream = null ;
202
+ InputStream tempFileInputStream = null ;
203
+ ContentInfo contentInfo = null ;
200
204
String notificationTag = contribution .getLocalUri ().toString ();
201
205
202
206
try {
203
- //FIXME: Google Photos bug
204
207
File file1 = new File (contribution .getLocalUri ().getPath ());
205
208
fileInputStream = new FileInputStream (file1 );
206
- //fileInputStream = this.getContentResolver().openInputStream(contribution.getLocalUri());
209
+ tempFileInputStream = new FileInputStream (file1 );
210
+ if (contentInfoUtil == null ) {
211
+ contentInfoUtil = new ContentInfoUtil ();
212
+ }
213
+ contentInfo = contentInfoUtil .findMatch (tempFileInputStream );
207
214
} catch (FileNotFoundException e ) {
208
215
Timber .d ("File not found" );
209
216
Toast fileNotFound = Toast .makeText (this , R .string .upload_failed , Toast .LENGTH_LONG );
210
217
fileNotFound .show ();
211
218
return ;
219
+ } catch (IOException e ) {
220
+ Timber .d ("exception while fetching MIME type: " +e );
221
+ } finally {
222
+ try {
223
+ if (null != tempFileInputStream ) {
224
+ tempFileInputStream .close ();
225
+ }
226
+ } catch (IOException e ) {
227
+ Timber .d ("File not found" );
228
+ }
212
229
}
213
230
214
231
//As the fileInputStream is null there's no point in continuing the upload process
@@ -226,9 +243,17 @@ private void uploadContribution(Contribution contribution) {
226
243
227
244
String filename = null ;
228
245
try {
246
+ //try to fetch the MIME type from contentInfo first and then use the tag to do it
247
+ //Note : the tag has not proven trustworthy in the past
248
+ String mimeType ;
249
+ if (contentInfo == null || contentInfo .getMimeType () == null ) {
250
+ mimeType = (String ) contribution .getTag ("mimeType" );
251
+ } else {
252
+ mimeType = contentInfo .getMimeType ();
253
+ }
229
254
filename = Utils .fixExtension (
230
255
contribution .getFilename (),
231
- MimeTypeMap .getSingleton ().getExtensionFromMimeType (( String ) contribution . getTag ( " mimeType" ) ));
256
+ MimeTypeMap .getSingleton ().getExtensionFromMimeType (mimeType ));
232
257
233
258
synchronized (unfinishedUploads ) {
234
259
Timber .d ("making sure of uniqueness of name: %s" , filename );
0 commit comments