Answers.Body: <p>While Craft 3 will be smarter about this, this is not currently possible in Craft 2.</p>
  
  <p>What you can do, though, is modify <code>craft/app/helpers/IOHelper.php::getMimeType()</code> function so that it always returns the mime type by extension instead by the content, since the core problem is that FileInfo extension cannot detect JSON by looking at the file contents. I'm a bit doubtful of any future releases for Craft 2 that affect this file, but, for posterity, probably better to write down any changes you make somewhere in case they get reverted.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-02T18:25:25.227Z
Answers.Text: Thanks Andris! For now we will likely run a script outside of Craft that adjusts the content-type so we don't interfere with any updates. Looking forward to the Craft 3 improvements!
Answers.UserDisplayName: Aaron Berkowitz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-30T09:00:53.313Z
Answers.LastActivityDate: 2016-12-30T09:00:53.313Z
Answers.UserDisplayName: Andris Sevcenko
Body: <p>I have a plugin that creates a local JSON file and uploads it to S3 using <a href="https://craftcms.com/classreference/services/AssetsService#insertFileByLocalPath-detail" rel="nofollow noreferrer"><code>insertFileByLocalPath</code></a>. The file is created, saved, and uploaded to s3 successfully. However, S3 detects the content-type to be <code>text/plain</code> and not <code>application/json</code>. Does anyone know of a way to force the content-type when creating an asset? </p>
  
  <pre><code>    $jsonStr = json_encode($arrayToBecomeJson);
  
      $fileName = $myFileName . '.json';
  
      $tempPath = AssetsHelper::getTempFilePath('json');
  
      file_put_contents($tempPath, $jsonStr);
  
      $assetSaveSuccess = craft()-&gt;assets-&gt;insertFileByLocalPath(
          $tempPath,
          $fileName,
          $assetFolderId,
          AssetConflictResolution::Replace
      );
  </code></pre>
  
Tags: <plugin-development><assets><s3>
Title: Asset Upload via Plugin - S3 gets wrong Content-Type
ThreadText: # Asset Upload via Plugin - S3 gets wrong Content-Type
  
  (Asked by: username_0 on 2016-12-29)
  
  I have a plugin that creates a local JSON file and uploads it to S3 using [`insertFileByLocalPath`](https://craftcms.com/classreference/services/AssetsService#insertFileByLocalPath-detail). The file is created, saved, and uploaded to s3 successfully. However, S3 detects the content-type to be `text/plain` and not `application/json`. Does anyone know of a way to force the content-type when creating an asset?
  
  ```
      $jsonStr = json_encode($arrayToBecomeJson);
  
      $fileName = $myFileName . '.json';
  
      $tempPath = AssetsHelper::getTempFilePath('json');
  
      file_put_contents($tempPath, $jsonStr);
  
      $assetSaveSuccess = craft()->assets->insertFileByLocalPath(
          $tempPath,
          $fileName,
          $assetFolderId,
          AssetConflictResolution::Replace
      );
  
  ```
  
  **Tags:** plugin-development, assets, s3
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-12-30. Score: 1 | Accepted answer
  
  While Craft 3 will be smarter about this, this is not currently possible in Craft 2.
  
  What you can do, though, is modify `craft/app/helpers/IOHelper.php::getMimeType()` function so that it always returns the mime type by extension instead by the content, since the core problem is that FileInfo extension cannot detect JSON by looking at the file contents. I'm a bit doubtful of any future releases for Craft 2 that affect this file, but, for posterity, probably better to write down any changes you make somewhere in case they get reverted.
  
  > **Comments:**
  > **username_0**: Thanks username_1! For now we will likely run a script outside of Craft that adjusts the content-type so we don't interfere with any updates. Looking forward to the Craft 3 improvements!  
  
  ---
  
  