Skip to content

Commit 5b3804b

Browse files
committed
上传同名文件提示覆盖
1 parent bef8a2f commit 5b3804b

1 file changed

Lines changed: 53 additions & 4 deletions

File tree

app/src/main/java/net/coding/program/project/detail/AttachmentsActivity.java

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ public class AttachmentsActivity extends FileDownloadBaseActivity implements Foo
8080
final public static int FILE_SELECT_CODE = 10;
8181
final public static int FILE_DELETE_CODE = 11;
8282
final public static int FILE_MOVE_CODE = 12;
83+
84+
private static final String TAG_HTTP_FILE_EXIST = "TAG_HTTP_FILE_EXIST";
85+
8386
private static String TAG = AttachmentsActivity.class.getSimpleName();
8487
@Extra
8588
int mProjectObjectId;
@@ -597,7 +600,7 @@ public void loadMore() {
597600
}
598601

599602
@Override
600-
public void parseJson(int code, JSONObject respanse, String tag, int pos, Object data) throws JSONException {
603+
public void parseJson(int code, JSONObject respanse, String tag, int pos, final Object data) throws JSONException {
601604
if (tag.equals(urlFiles)) {
602605
if (code == 0) {
603606
JSONArray files = respanse.getJSONObject("data").getJSONArray("list");
@@ -722,6 +725,23 @@ public void parseJson(int code, JSONObject respanse, String tag, int pos, Object
722725
} else {
723726
showErrorMsg(code, respanse);
724727
}
728+
} else if (tag.equals(TAG_HTTP_FILE_EXIST)) {
729+
if (code == 0) {
730+
String s = respanse.optJSONObject("data").optString("conflict_file");
731+
if (s.isEmpty()) {
732+
uploadFile((File) data);
733+
} else {
734+
File file = (File) data;
735+
showDialog(file.getName(), "存在同名文件,是否覆盖?", new DialogInterface.OnClickListener() {
736+
@Override
737+
public void onClick(DialogInterface dialog, int which) {
738+
uploadFile((File) data);
739+
}
740+
});
741+
}
742+
} else {
743+
showErrorMsg(code, respanse);
744+
}
725745
}
726746
}
727747

@@ -747,10 +767,15 @@ void onResult(int resultCode, Intent data) {
747767
Uri uri = data.getData();
748768
String path = FileUtil.getPath(this, uri);
749769
File selectedFile = new File(path);
750-
uploadFile(selectedFile);
770+
uploadFilePrepare(selectedFile);
751771
}
752772
}
753773

774+
private void uploadFilePrepare(File selectedFile) {
775+
String httpHost = getHttpFileExist(selectedFile.getName(), mAttachmentFolderObject);
776+
getNetwork(httpHost, TAG_HTTP_FILE_EXIST, -1, selectedFile);
777+
}
778+
754779
private void uploadFile(File selectedFile) {
755780

756781
try {
@@ -781,7 +806,20 @@ public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
781806
if (code == 0) {
782807
AttachmentFileObject newFile = new AttachmentFileObject(response.getJSONObject("data"));
783808
setDownloadStatus(newFile);
784-
mFilesArray.add(mAttachmentFolderObject.sub_folders.size(), newFile);
809+
810+
int i = 0;
811+
for (; i < mFilesArray.size(); ++i) {
812+
AttachmentFileObject item = mFilesArray.get(i);
813+
String itemName = item.getName();
814+
if (!item.isFolder && itemName.equals(newFile.getName())) {
815+
mFilesArray.set(i, newFile);
816+
break;
817+
}
818+
}
819+
if (i == mFilesArray.size()) {
820+
mFilesArray.add(mAttachmentFolderObject.sub_folders.size(), newFile);
821+
}
822+
785823
adapter.notifyDataSetChanged();
786824
setResult(Activity.RESULT_OK);
787825
showUploadStatus(UploadStatus.Finish);
@@ -829,6 +867,18 @@ public void onProgress(int bytesWritten, int totalSize) {
829867

830868
}
831869

870+
871+
private String getHttpFileExist(String name, AttachmentFolderObject folder) {
872+
String encodeName = Global.encodeUtf8(name);
873+
return Global.HOST_API +
874+
mProject.getProjectPath() +
875+
"/dir/" +
876+
folder.file_id +
877+
"/files/existed?names=" +
878+
encodeName;
879+
}
880+
881+
832882
private void showUploadStatus(UploadStatus status) {
833883
switch (status) {
834884
case Uploading:
@@ -1368,7 +1418,6 @@ public void checkFileDownloadStatus() {
13681418
}
13691419

13701420

1371-
13721421
protected String getLink() {
13731422
if (mProjectObject == null) {
13741423
showButtomToast("获取项目信息失败,请稍后重试");

0 commit comments

Comments
 (0)