{"action":"created","issue":{"url":"https://api.github.com/repos/josegonzalez/cakephp-upload/issues/265","labels_url":"https://api.github.com/repos/josegonzalez/cakephp-upload/issues/265/labels{/name}","comments_url":"https://api.github.com/repos/josegonzalez/cakephp-upload/issues/265/comments","events_url":"https://api.github.com/repos/josegonzalez/cakephp-upload/issues/265/events","html_url":"https://github.com/josegonzalez/cakephp-upload/issues/265","id":49910489,"number":265,"title":"Why are you uploading a new file before removing the ones who should be deleted ?","user":{"login":"CaptainQuirk","id":993420,"avatar_url":"https://avatars.githubusercontent.com/u/993420?v=3","gravatar_id":"","url":"https://api.github.com/users/CaptainQuirk","html_url":"https://github.com/CaptainQuirk","followers_url":"https://api.github.com/users/CaptainQuirk/followers","following_url":"https://api.github.com/users/CaptainQuirk/following{/other_user}","gists_url":"https://api.github.com/users/CaptainQuirk/gists{/gist_id}","starred_url":"https://api.github.com/users/CaptainQuirk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CaptainQuirk/subscriptions","organizations_url":"https://api.github.com/users/CaptainQuirk/orgs","repos_url":"https://api.github.com/users/CaptainQuirk/repos","events_url":"https://api.github.com/users/CaptainQuirk/events{/privacy}","received_events_url":"https://api.github.com/users/CaptainQuirk/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2014-11-24T16:27:23Z","updated_at":"2015-02-20T14:26:26Z","closed_at":null,"body":"Hi there,\r\n\r\nFirst, thanks for your work, you save us hours and hours !\r\n\r\nBefore I go through the pull request process, I was wondering what design decision made you unlink files to be removed after having handled the current file upload. Are there any reasons why you didn't call __unlinkFiles on top of the behavior's afterSave function ? \r\n\r\nWithout removing files beforehand, it is impossible to replace a file with a same name. I've seen that you've been adding the ability to create a callback to handle the file upload manually, but to me, replacing an existing file with a new one with the same name is valid enough a use case to include it in the Behavior.\r\n\r\nResulting code would look like the following\r\n\r\n```\r\n<?php\r\n/**\r\n * After save method. Called after all saves\r\n *\r\n * Handles moving file uploads\r\n *\r\n * @param Model $model Model instance\r\n * @param bool $created True if this save created a new record\r\n * @param array $options Options passed from Model::save().\r\n * @return bool\r\n * @throws UploadException\r\n */\r\n  public function afterSave(Model $model, $created, $options = array()) {\r\n    $temp = array($model->alias => array());\r\n\r\n    $this->_unlinkFiles($model);\r\n    foreach ($this->settings[$model->alias] as $field => $options) {\r\n      if ($this->_shouldSkip($model, $field)) {\r\n        continue;\r\n      }\r\n\r\n      $tempPath = $this->_getPath($model, $field);\r\n\r\n      $path = $this->settings[$model->alias][$field]['path'];\r\n      $thumbnailPath = $this->settings[$model->alias][$field]['thumbnailPath'];\r\n\r\n      if (!empty($tempPath)) {\r\n        $path .= $tempPath . DS;\r\n        $thumbnailPath .= $tempPath . DS;\r\n      }\r\n\r\n      $tmp = $this->runtime[$model->alias][$field]['tmp_name'];\r\n      $filePath = $path . $model->data[$model->alias][$field];\r\n      if (!$this->handleUploadedFile($model, $field, $tmp, $filePath)) {\r\n        CakeLog::error(sprintf('Model %s, Field %s: Unable to move the uploaded file to %s', $model->alias, $field, $filePath));\r\n        $model->invalidate($field, sprintf('Unable to move the uploaded file to %s', $filePath));\r\n        $db = $model->getDataSource();\r\n        $db->rollback();\r\n        throw new UploadException('Unable to upload file');\r\n      }\r\n\r\n      $this->_createThumbnails($model, $field, $path, $thumbnailPath);\r\n      if ($model->hasField($options['fields']['dir'])) {\r\n        if (!($created && $options['pathMethod'] == '_getPathFlat') && $options['saveDir']) {\r\n          $db = $model->getDataSource();\r\n          $temp[$model->alias][$options['fields']['dir']] = $db->value($tempPath, 'string');\r\n        }\r\n      }\r\n    }\r\n\r\n    $this->_updateRecord($model, $temp);\r\n\r\n    return;\r\n  }\r\n```\r\n\r\nWhat do you think of it ?"},"comment":{"url":"https://api.github.com/repos/josegonzalez/cakephp-upload/issues/comments/75245480","html_url":"https://github.com/josegonzalez/cakephp-upload/issues/265#issuecomment-75245480","issue_url":"https://api.github.com/repos/josegonzalez/cakephp-upload/issues/265","id":75245480,"user":{"login":"davidyell","id":49889,"avatar_url":"https://avatars.githubusercontent.com/u/49889?v=3","gravatar_id":"","url":"https://api.github.com/users/davidyell","html_url":"https://github.com/davidyell","followers_url":"https://api.github.com/users/davidyell/followers","following_url":"https://api.github.com/users/davidyell/following{/other_user}","gists_url":"https://api.github.com/users/davidyell/gists{/gist_id}","starred_url":"https://api.github.com/users/davidyell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidyell/subscriptions","organizations_url":"https://api.github.com/users/davidyell/orgs","repos_url":"https://api.github.com/users/davidyell/repos","events_url":"https://api.github.com/users/davidyell/events{/privacy}","received_events_url":"https://api.github.com/users/davidyell/received_events","type":"User","site_admin":false},"created_at":"2015-02-20T14:26:26Z","updated_at":"2015-02-20T14:26:26Z","body":"Relates to #191 "}}