Example #1
0
  public TranscodeFileImpl lookupFile(TranscodeProfile profile, DiskManagerFileInfo file) {
    try {
      synchronized (this) {
        if (device_files == null) {

          loadDeviceFile();
        }

        String key =
            ByteFormatter.encodeString(file.getDownloadHash())
                + ":"
                + file.getIndex()
                + ":"
                + profile.getUID();

        if (device_files.containsKey(key)) {

          try {
            return (new TranscodeFileImpl(this, key, device_files));

          } catch (Throwable e) {

            device_files.remove(key);

            log("Failed to deserialise transcode file", e);
          }
        }
      }
    } catch (Throwable e) {
    }

    return (null);
  }
Example #2
0
  protected TranscodeFileImpl getTranscodeFile(String key) {
    try {
      synchronized (this) {
        if (device_files == null) {

          loadDeviceFile();
        }

        if (device_files.containsKey(key)) {

          try {

            return (new TranscodeFileImpl(this, key, device_files));

          } catch (Throwable e) {

            device_files.remove(key);

            log("Failed to deserialise transcode file", e);
          }
        }
      }
    } catch (Throwable e) {
    }

    return (null);
  }
Example #3
0
  protected void fileDirty(TranscodeFileImpl file, int type, Object data) {
    try {
      synchronized (this) {
        if (device_files == null) {

          loadDeviceFile();

        } else {

          device_files_last_mod = SystemTime.getMonotonousTime();
        }
      }

      device_files_dirty = true;

    } catch (Throwable e) {

      Debug.out("Failed to load device file", e);
    }

    for (TranscodeTargetListener l : listeners) {

      try {
        l.fileChanged(file, type, data);

      } catch (Throwable e) {

        Debug.out(e);
      }
    }
  }
Example #4
0
  protected void revertFileName(TranscodeFileImpl tf) throws TranscodeException {

    File cache_file = tf.getCacheFile();

    if (cache_file.exists()) {

      Debug.out("Cache file already allocated, can't rename");

      return;
    }

    File source_file = tf.getSourceFile().getFile(true);

    String original_name = source_file.getName();

    int pos = original_name.indexOf('.');

    if (pos == -1) {

      return;
    }

    String cf_name = cache_file.getName();

    if (cf_name.endsWith(original_name.substring(pos))) {

      return;
    }

    try {
      synchronized (this) {
        if (device_files == null) {

          loadDeviceFile();
        }

        String reverted_name = allocateUniqueFileName(original_name);

        tf.setCacheFile(new File(cache_file.getParentFile(), reverted_name));
      }
    } catch (Throwable e) {

      throw (new TranscodeException("File name revertion failed", e));
    }
  }
Example #5
0
  public int getFileCount() {
    try {
      synchronized (this) {
        if (device_files == null) {

          loadDeviceFile();
        }

        return device_files.size();
      }

    } catch (Throwable e) {

      Debug.out("Failed to load device file", e);
    }

    return 0;
  }
Example #6
0
  protected void saveDeviceFile() {
    device_files_dirty = false;

    try {
      loadDeviceFile();

      if (device_files == null || device_files.size() == 0) {

        FileUtil.deleteResilientFile(getDeviceFile());

      } else {
        Map map = new HashMap();

        map.put("files", device_files);

        FileUtil.writeResilientFile(getDeviceFile(), map);
      }
    } catch (Throwable e) {

      Debug.out("Failed to save device file", e);
    }
  }
Example #7
0
  public TranscodeFileImpl[] getFiles() {
    try {
      synchronized (this) {
        if (device_files == null) {

          loadDeviceFile();
        }

        List<TranscodeFile> result = new ArrayList<TranscodeFile>();

        Iterator<Map.Entry<String, Map<String, ?>>> it = device_files.entrySet().iterator();

        while (it.hasNext()) {

          Map.Entry<String, Map<String, ?>> entry = it.next();

          try {
            TranscodeFileImpl tf = new TranscodeFileImpl(this, entry.getKey(), device_files);

            result.add(tf);

          } catch (Throwable e) {

            it.remove();

            log("Failed to deserialise transcode file", e);
          }
        }

        return (result.toArray(new TranscodeFileImpl[result.size()]));
      }
    } catch (Throwable e) {

      Debug.out(e);

      return (new TranscodeFileImpl[0]);
    }
  }
Example #8
0
  public TranscodeFileImpl allocateFile(
      TranscodeProfile profile, boolean no_xcode, DiskManagerFileInfo file, boolean for_job)
      throws TranscodeException {
    TranscodeFileImpl result = null;

    setError(KEY_FILE_ALLOC_ERROR, null);

    try {
      synchronized (this) {
        if (device_files == null) {

          loadDeviceFile();
        }

        String key =
            ByteFormatter.encodeString(file.getDownloadHash())
                + ":"
                + file.getIndex()
                + ":"
                + profile.getUID();

        if (device_files.containsKey(key)) {

          try {
            result = new TranscodeFileImpl(this, key, device_files);

          } catch (Throwable e) {

            device_files.remove(key);

            log("Failed to deserialise transcode file", e);
          }
        }

        if (result == null) {

          String ext = profile.getFileExtension();

          String target_file = file.getFile(true).getName();

          if (ext != null && !no_xcode) {

            int pos = target_file.lastIndexOf('.');

            if (pos != -1) {

              target_file = target_file.substring(0, pos);
            }

            target_file += ext;
          }

          target_file = allocateUniqueFileName(target_file);

          File output_file = getWorkingDirectory(true);

          if (!output_file.canWrite()) {

            throw (new TranscodeException(
                "Can't write to transcode folder '" + output_file.getAbsolutePath() + "'"));
          }

          output_file = new File(output_file.getAbsoluteFile(), target_file);

          result =
              new TranscodeFileImpl(
                  this, key, profile.getName(), device_files, output_file, for_job);

          result.setSourceFile(file);

          device_files_last_mod = SystemTime.getMonotonousTime();

          device_files_dirty = true;

        } else {

          result.setSourceFile(file);

          result.setProfileName(profile.getName());
        }
      }
    } catch (Throwable e) {

      setError(KEY_FILE_ALLOC_ERROR, Debug.getNestedExceptionMessage(e));

      throw (new TranscodeException("File allocation failed", e));
    }

    for (TranscodeTargetListener l : listeners) {

      try {
        l.fileAdded(result);

      } catch (Throwable e) {

        Debug.out(e);
      }
    }

    return (result);
  }
Example #9
0
  protected void deleteFile(TranscodeFileImpl file, boolean delete_contents, boolean remove)
      throws TranscodeException {
    if (file.isDeleted()) {

      return;
    }

    if (delete_contents) {

      File f = file.getCacheFile();

      int time = 0;

      while (f.exists() && !f.delete()) {

        if (time > 3000) {

          log("Failed to remove file '" + f.getAbsolutePath() + "'");

          break;

        } else {

          try {
            Thread.sleep(500);

          } catch (Throwable e) {

          }

          time += 500;
        }
      }
    }

    if (remove) {

      try {
        // fire the listeners FIRST as this gives listeners a chance to extract data
        // from the file before it is deleted (otherwise operations fail with 'file has been
        // deleted'

        for (TranscodeTargetListener l : listeners) {

          try {
            l.fileRemoved(file);

          } catch (Throwable e) {

            Debug.out(e);
          }
        }

        synchronized (this) {
          if (device_files == null) {

            loadDeviceFile();

          } else {

            device_files_last_mod = SystemTime.getMonotonousTime();
          }

          device_files.remove(file.getKey());

          device_files_dirty = true;
        }

      } catch (Throwable e) {

        throw (new TranscodeException("Delete failed", e));
      }
    }
  }