View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.app.web.api.admin.storage;
17  
18  import static org.codelibs.fess.app.web.admin.storage.AdminStorageAction.decodeId;
19  import static org.codelibs.fess.app.web.admin.storage.AdminStorageAction.decodePath;
20  import static org.codelibs.fess.app.web.admin.storage.AdminStorageAction.deleteObject;
21  import static org.codelibs.fess.app.web.admin.storage.AdminStorageAction.downloadObject;
22  import static org.codelibs.fess.app.web.admin.storage.AdminStorageAction.getFileItems;
23  import static org.codelibs.fess.app.web.admin.storage.AdminStorageAction.getObjectName;
24  import static org.codelibs.fess.app.web.admin.storage.AdminStorageAction.uploadObject;
25  
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.logging.log4j.LogManager;
30  import org.apache.logging.log4j.Logger;
31  import org.codelibs.core.lang.StringUtil;
32  import org.codelibs.fess.app.web.api.ApiResult;
33  import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
34  import org.codelibs.fess.exception.ResultOffsetExceededException;
35  import org.codelibs.fess.exception.StorageException;
36  import org.dbflute.optional.OptionalThing;
37  import org.lastaflute.web.Execute;
38  import org.lastaflute.web.response.JsonResponse;
39  import org.lastaflute.web.response.StreamResponse;
40  
41  public class ApiAdminStorageAction extends FessApiAdminAction {
42  
43      private static final Logger logger = LogManager.getLogger(ApiAdminStorageAction.class);
44  
45      // GET /api/admin/storage/list/{id}
46      // POST /api/admin/storage/list/{id}
47      @Execute
48      public JsonResponse<ApiResult> list(final OptionalThing<String> id) {
49          final List<Map<String, Object>> list = getFileItems(id.isPresent() ? decodePath(id.get()) : null);
50          try {
51              return asJson(new ApiResult.ApiStorageResponse().items(list).status(ApiResult.Status.OK).result());
52          } catch (final ResultOffsetExceededException e) {
53              if (logger.isDebugEnabled()) {
54                  logger.debug(e.getMessage(), e);
55              }
56              throwValidationErrorApi(messages -> messages.addErrorsResultSizeExceeded(GLOBAL));
57          }
58  
59          return null;
60      }
61  
62      // GET /api/admin/storage/download/{id}/
63      @Execute
64      public StreamResponse get$download(final String id) {
65          final String[] values = decodeId(id);
66          if (StringUtil.isEmpty(values[1])) {
67              throwValidationErrorApi(messages -> messages.addErrorsStorageFileNotFound(GLOBAL));
68          }
69          return asStream(values[1]).contentTypeOctetStream().stream(out -> {
70              try {
71                  downloadObject(getObjectName(values[0], values[1]), out);
72              } catch (final StorageException e) {
73                  if (logger.isDebugEnabled()) {
74                      logger.debug("Failed to download {}", id, e);
75                  }
76                  throwValidationErrorApi(messages -> messages.addErrorsStorageFileDownloadFailure(GLOBAL, values[1]));
77              }
78          });
79      }
80  
81      // DELETE /api/admin/storage/delete/{id}/
82      @Execute
83      public JsonResponse<ApiResult> delete$delete(final String id) {
84          final String[] values = decodeId(id);
85          if (StringUtil.isEmpty(values[1])) {
86              throwValidationErrorApi(messages -> messages.addErrorsStorageAccessError(GLOBAL, "id is invalid"));
87          }
88          final String objectName = getObjectName(values[0], values[1]);
89          try {
90              deleteObject(objectName);
91              saveInfo(messages -> messages.addSuccessDeleteFile(GLOBAL, values[1]));
92              return asJson(new ApiResult.ApiResponse().status(ApiResult.Status.OK).result());
93          } catch (final StorageException e) {
94              if (logger.isDebugEnabled()) {
95                  logger.debug("Failed to delete {}", id, e);
96              }
97              throwValidationErrorApi(messages -> messages.addErrorsFailedToDeleteFile(GLOBAL, values[1]));
98          }
99          return null;
100     }
101 
102     // curl -XPOST -H "Authorization: CHANGEME" localhost:8080/api/admin/storage/upload/ -F path=/ -F file=@...
103     // POST /api/admin/storage/upload/{pathId}/
104     @Execute
105     public JsonResponse<ApiResult> post$upload(final UploadForm form) {
106         validateApi(form, messages -> {});
107         if (form.file == null) {
108             throwValidationErrorApi(messages -> messages.addErrorsStorageNoUploadFile(GLOBAL));
109         }
110         final String fileName = form.file.getFileName();
111         try {
112             uploadObject(getObjectName(form.path, fileName), form.file);
113             saveInfo(messages -> messages.addSuccessUploadFileToStorage(GLOBAL, fileName));
114             return asJson(new ApiResult.ApiResponse().status(ApiResult.Status.OK).result());
115         } catch (final StorageException e) {
116             if (logger.isDebugEnabled()) {
117                 logger.debug("Failed to upload {}", fileName, e);
118             }
119             throwValidationErrorApi(messages -> messages.addErrorsStorageFileUploadFailure(GLOBAL, fileName));
120         }
121         return null;
122     }
123 
124 }