mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 10:27:45 +00:00 
			
		
		
		
	Include upload headers in interceptor and finish upload file operation
This commit is contained in:
		
							parent
							
								
									75ce8e186f
								
							
						
					
					
						commit
						63c917c979
					
				| @ -1 +1 @@ | |||||||
| Subproject commit 6a5835a960c4a3d4361a70ef72914b9fa214363d | Subproject commit d0a09ad1ea87044d7e50cc870fc798562d1f0d38 | ||||||
| @ -37,10 +37,11 @@ public abstract class RemoteOperation { | |||||||
|         return mClient.newBuilder() |         return mClient.newBuilder() | ||||||
|                 .addInterceptor(chain -> |                 .addInterceptor(chain -> | ||||||
|                         chain.proceed( |                         chain.proceed( | ||||||
|                                 addRequestCredentials( |                                 addRequestCredentials(chain.request()) | ||||||
|                                         chain.request()) |  | ||||||
|                                         .addHeader(USER_AGENT_HEADER, mContext.getUserAgent()) |                                         .addHeader(USER_AGENT_HEADER, mContext.getUserAgent()) | ||||||
|                                         .build())) |                                         .build() | ||||||
|  |                         ) | ||||||
|  |                 ) | ||||||
|                 .followRedirects(false) |                 .followRedirects(false) | ||||||
|                 .build(); |                 .build(); | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -30,23 +30,33 @@ import com.owncloud.android.lib.refactor.RemoteOperationResult; | |||||||
| 
 | 
 | ||||||
| import java.io.File; | import java.io.File; | ||||||
| 
 | 
 | ||||||
| import at.bitfire.dav4android.DavOCResource; | import at.bitfire.dav4android.DavResource; | ||||||
| import okhttp3.MediaType; | import okhttp3.MediaType; | ||||||
|  | import okhttp3.Request; | ||||||
| import okhttp3.RequestBody; | import okhttp3.RequestBody; | ||||||
| 
 | 
 | ||||||
|  | /** | ||||||
|  |  * @author David González Verdugo | ||||||
|  |  */ | ||||||
| public class UploadRemoteFileOperation extends RemoteOperation { | public class UploadRemoteFileOperation extends RemoteOperation { | ||||||
| 
 | 
 | ||||||
|     private String mLocalPath; |     private static final String CONTENT_TYPE_HEADER = "Content-Type"; | ||||||
|  |     private static final String OC_TOTAL_LENGTH_HEADER = "OC-Total-Length"; | ||||||
|  |     private static final String OC_X_OC_MTIME_HEADER = "X-OC-Mtime"; | ||||||
|  |     private static final String IF_MATCH_HEADER = "If-Match"; | ||||||
|  | 
 | ||||||
|  |     private File mFileToUpload; | ||||||
|     private String mRemotePath; |     private String mRemotePath; | ||||||
|     private String mMimeType; |     private String mMimeType; | ||||||
|     private String mFileLastModifTimestamp; |     private String mFileLastModifTimestamp; | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|     public UploadRemoteFileOperation(OCContext context, String localPath, String remotePath, String mimetype, |     public UploadRemoteFileOperation(OCContext context, String localPath, String remotePath, String mimetype, | ||||||
|                                      String fileLastModifTimestamp) { |                                      String fileLastModifTimestamp) { | ||||||
|         super(context); |         super(context); | ||||||
| 
 | 
 | ||||||
|         mLocalPath = localPath; |         mFileToUpload = new File(localPath); | ||||||
|         mRemotePath = remotePath; |         mRemotePath = remotePath.replaceAll("^/+", ""); //Delete leading slashes | ||||||
|         mMimeType = mimetype; |         mMimeType = mimetype; | ||||||
|         mFileLastModifTimestamp = fileLastModifTimestamp; |         mFileLastModifTimestamp = fileLastModifTimestamp; | ||||||
|     } |     } | ||||||
| @ -56,21 +66,25 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
| 
 | 
 | ||||||
|         try { |         try { | ||||||
| 
 | 
 | ||||||
|             File fileToUpload = new File(mLocalPath); |  | ||||||
|             MediaType mediaType = MediaType.parse(mMimeType); |             MediaType mediaType = MediaType.parse(mMimeType); | ||||||
|             RequestBody requestBody = RequestBody.create(mediaType, fileToUpload); |             RequestBody requestBody = RequestBody.create(mediaType, mFileToUpload); | ||||||
| 
 | 
 | ||||||
|             DavOCResource davOCResource = new DavOCResource( |             DavResource davResource = new DavResource( | ||||||
|                     getClient(), |                     getClient() | ||||||
|                     getWebDavHttpUrl(mRemotePath), |                             .newBuilder() | ||||||
|                     null); |                             .addInterceptor(chain -> | ||||||
|  |                                     chain.proceed( | ||||||
|  |                                             addUploadHeaders(chain.request()) | ||||||
|  |                                             .build() | ||||||
|  |                                     ) | ||||||
|  |                             ) | ||||||
|  |                             .followRedirects(false) | ||||||
|  |                             .build(), | ||||||
|  |                     getWebDavHttpUrl(mRemotePath)); | ||||||
| 
 | 
 | ||||||
|             davOCResource.put(requestBody, |             davResource.put(requestBody, | ||||||
|                     null, |                     null, | ||||||
|                     false, |                     false); | ||||||
|                     "multipart/form-data", |  | ||||||
|                     String.valueOf(fileToUpload.length()), |  | ||||||
|                     mFileLastModifTimestamp); |  | ||||||
| 
 | 
 | ||||||
|         } catch (Exception e) { |         } catch (Exception e) { | ||||||
|             e.printStackTrace(); |             e.printStackTrace(); | ||||||
| @ -78,4 +92,19 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
| 
 | 
 | ||||||
|         return null; |         return null; | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Add headers needed to upload a file | ||||||
|  |      * @param request request in which include the headers | ||||||
|  |      * @return request with upload headers | ||||||
|  |      */ | ||||||
|  |     private Request.Builder addUploadHeaders (Request request) { | ||||||
|  | 
 | ||||||
|  |         Request.Builder builder =  request.newBuilder(); | ||||||
|  |         builder.addHeader(CONTENT_TYPE_HEADER, "multipart/form-data"); | ||||||
|  |         builder.addHeader(OC_TOTAL_LENGTH_HEADER, String.valueOf(mFileToUpload.length())); | ||||||
|  |         builder.addHeader(OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp); | ||||||
|  | 
 | ||||||
|  |         return builder; | ||||||
|  |     } | ||||||
| } | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user