mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +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() | ||||
|                 .addInterceptor(chain -> | ||||
|                         chain.proceed( | ||||
|                                 addRequestCredentials( | ||||
|                                         chain.request()) | ||||
|                                 addRequestCredentials(chain.request()) | ||||
|                                         .addHeader(USER_AGENT_HEADER, mContext.getUserAgent()) | ||||
|                                         .build())) | ||||
|                                         .build() | ||||
|                         ) | ||||
|                 ) | ||||
|                 .followRedirects(false) | ||||
|                 .build(); | ||||
|     } | ||||
|  | ||||
| @ -30,23 +30,33 @@ import com.owncloud.android.lib.refactor.RemoteOperationResult; | ||||
| 
 | ||||
| import java.io.File; | ||||
| 
 | ||||
| import at.bitfire.dav4android.DavOCResource; | ||||
| import at.bitfire.dav4android.DavResource; | ||||
| import okhttp3.MediaType; | ||||
| import okhttp3.Request; | ||||
| import okhttp3.RequestBody; | ||||
| 
 | ||||
| /** | ||||
|  * @author David González Verdugo | ||||
|  */ | ||||
| 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 mMimeType; | ||||
|     private String mFileLastModifTimestamp; | ||||
| 
 | ||||
| 
 | ||||
|     public UploadRemoteFileOperation(OCContext context, String localPath, String remotePath, String mimetype, | ||||
|                                      String fileLastModifTimestamp) { | ||||
|         super(context); | ||||
| 
 | ||||
|         mLocalPath = localPath; | ||||
|         mRemotePath = remotePath; | ||||
|         mFileToUpload = new File(localPath); | ||||
|         mRemotePath = remotePath.replaceAll("^/+", ""); //Delete leading slashes | ||||
|         mMimeType = mimetype; | ||||
|         mFileLastModifTimestamp = fileLastModifTimestamp; | ||||
|     } | ||||
| @ -56,21 +66,25 @@ public class UploadRemoteFileOperation extends RemoteOperation { | ||||
| 
 | ||||
|         try { | ||||
| 
 | ||||
|             File fileToUpload = new File(mLocalPath); | ||||
|             MediaType mediaType = MediaType.parse(mMimeType); | ||||
|             RequestBody requestBody = RequestBody.create(mediaType, fileToUpload); | ||||
|             RequestBody requestBody = RequestBody.create(mediaType, mFileToUpload); | ||||
| 
 | ||||
|             DavOCResource davOCResource = new DavOCResource( | ||||
|                     getClient(), | ||||
|                     getWebDavHttpUrl(mRemotePath), | ||||
|                     null); | ||||
|             DavResource davResource = new DavResource( | ||||
|                     getClient() | ||||
|                             .newBuilder() | ||||
|                             .addInterceptor(chain -> | ||||
|                                     chain.proceed( | ||||
|                                             addUploadHeaders(chain.request()) | ||||
|                                             .build() | ||||
|                                     ) | ||||
|                             ) | ||||
|                             .followRedirects(false) | ||||
|                             .build(), | ||||
|                     getWebDavHttpUrl(mRemotePath)); | ||||
| 
 | ||||
|             davOCResource.put(requestBody, | ||||
|             davResource.put(requestBody, | ||||
|                     null, | ||||
|                     false, | ||||
|                     "multipart/form-data", | ||||
|                     String.valueOf(fileToUpload.length()), | ||||
|                     mFileLastModifTimestamp); | ||||
|                     false); | ||||
| 
 | ||||
|         } catch (Exception e) { | ||||
|             e.printStackTrace(); | ||||
| @ -78,4 +92,19 @@ public class UploadRemoteFileOperation extends RemoteOperation { | ||||
| 
 | ||||
|         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