/platform/platform-impl/src/com/intellij/openapi/vfs/impl/http/LocalFileStorage.java

https://bitbucket.org/nbargnesi/idea · Java · 64 lines · 39 code · 7 blank · 18 comment · 7 complexity · c1949e2d3b166acf3e6c885422849567 MD5 · raw file

  1. /*
  2. * Copyright 2000-2009 JetBrains s.r.o.
  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, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.openapi.vfs.impl.http;
  17. import com.intellij.openapi.application.PathManager;
  18. import com.intellij.openapi.util.io.FileUtil;
  19. import com.intellij.util.PathUtil;
  20. import org.jetbrains.annotations.NotNull;
  21. import java.io.File;
  22. import java.io.IOException;
  23. /**
  24. * @author nik
  25. */
  26. public class LocalFileStorage {
  27. private final File myStorageIODirectory;
  28. public LocalFileStorage() {
  29. myStorageIODirectory = new File(PathManager.getSystemPath(), "httpFileSystem");
  30. myStorageIODirectory.mkdirs();
  31. }
  32. public File createLocalFile(@NotNull String url) throws IOException {
  33. int ast = url.indexOf('?');
  34. if (ast != -1) {
  35. url = url.substring(0, ast);
  36. }
  37. int last = url.lastIndexOf('/');
  38. String baseName;
  39. if (last == url.length() - 1) {
  40. baseName = url.substring(url.lastIndexOf('/', last-1) + 1, last);
  41. }
  42. else {
  43. baseName = url.substring(last + 1);
  44. }
  45. int index = baseName.lastIndexOf('.');
  46. String prefix = index == -1 ? baseName : baseName.substring(0, index);
  47. String suffix = index == -1 ? "" : baseName.substring(index+1);
  48. prefix = PathUtil.suggestFileName(prefix);
  49. suffix = PathUtil.suggestFileName(suffix);
  50. File file = FileUtil.findSequentNonexistentFile(myStorageIODirectory, prefix, suffix);
  51. FileUtil.createIfDoesntExist(file);
  52. return file;
  53. }
  54. public void deleteDownloadedFiles() {
  55. FileUtil.delete(myStorageIODirectory);
  56. }
  57. }