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