001/* 002 * Syncany, www.syncany.org 003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com> 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU General Public License as published by 007 * the Free Software Foundation, either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package org.syncany.plugins.transfer.files; 019 020import java.util.regex.Matcher; 021import java.util.regex.Pattern; 022 023import org.syncany.crypto.CipherUtil; 024import org.syncany.plugins.transfer.StorageException; 025 026/** 027 * The temp file represents a temporary file on the remote storage. 028 * 029 * <p><b>Name pattern:</b> The name pattern of a temp file is 030 * <b>temp-<randomidentifier></b>. 031 * 032 * @author Pim Otte 033 */ 034public class TempRemoteFile extends RemoteFile { 035 private static final Pattern NAME_PATTERN = Pattern.compile("temp-([A-Za-z]+)-(.+)"); 036 private static final String NAME_FORMAT = "temp-%s-%s"; 037 038 private RemoteFile targetRemoteFile; 039 040 /** 041 * Initializes a new temp file, given a name. 042 * 043 * @param name temp file name; <b>must</b> always match the {@link #NAME_PATTERN} 044 * @throws StorageException If the name is not match the name pattern 045 */ 046 public TempRemoteFile(String name) throws StorageException { 047 super(name); 048 } 049 050 /** 051 * Initializes a new randomly named temp file. 052 * 053 * @throws StorageException 054 */ 055 public TempRemoteFile(RemoteFile targetRemoteFile) throws StorageException { 056 super(String.format(NAME_FORMAT, CipherUtil.createRandomAlphabeticString(5), targetRemoteFile.getName())); 057 } 058 059 /** 060 * Returns the target remote file, i.e. the {@link RemoteFile} this 061 * temporary file will be renamed into, or was renamed from. 062 */ 063 public RemoteFile getTargetRemoteFile() { 064 return targetRemoteFile; 065 } 066 067 @Override 068 protected String validateName(String name) throws StorageException { 069 Matcher matcher = NAME_PATTERN.matcher(name); 070 071 if (!matcher.matches()) { 072 throw new StorageException(name + ": remote filename pattern does not match: " + NAME_PATTERN.pattern() + " expected."); 073 } 074 075 try { 076 targetRemoteFile = RemoteFile.createRemoteFile(matcher.group(2)); 077 } 078 catch (Exception e) { 079 throw new StorageException(name + ": remote filename pattern does not match: " + NAME_PATTERN.pattern() + " expected."); 080 } 081 082 return name; 083 } 084}