# file.save()

Canonical URL: https://developers.iagenify.com/ar/sdk/ref/files/save/
Language: ar

`file.save({ storageId, path, localOnly }) → Promise<Result>`

يكتب الملف ، الى الحساب ، الى القرص المحلي ، او كليهما ، ويُدعى هنا ويكلِّف ذلك مبلغا .

Status: POST /api/files/save · 1 credit · refunded if the write fails

## المعاملات {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `storageId` | string |  | أيّ مخزن سيكتب إليه، بدونه الحساب افتراضيّ |
| `path` | string |  | Node فقط. اكتب أيضاً إلى هذا المسار المحلي، إنشاء المجلّدات. |
| `localOnly` | boolean |  | مع مع مع `path`: كتابة محلياً و **توقف**لا شيء يُرفع، لا شيء يُحاسب |

## الطرق الثلاث لاستخدامه {#three}

```javascript
// 1. To the account — the usual one
await file.save();

// 2. To the account AND to this machine, in one call
await file.save({ path: "./out/report.md" });

// 3. To this machine only — no request, no credit
await file.save({ path: "./out/report.md", localOnly: true });
// { ok: true, status: "saved_local", path: "./out/report.md" }
```

> **لا يُفِض هذا المُ_____ لا ي**
>
> إذا كان الطريق لا يمكن كتابته، SDK تحذيرات على لوحة المفاتيح واكمل إلى التحميل تحقق من الملف بنفسك عندما تكون النسخة المحلية هي التي تهم - مجلد للقراءة فقط لن يرفع.

## القيمة المُعادة {#returns}

```javascript
{
  ok: true,
  status: "saved",
  sourceId: "…",            // how you find it again
  name: "report.md",
  category: "document",
  mimeType: "text/markdown",
  location: "object",       // the kind of storage it landed in
  metadata: {
    size: 2048,
    storageId: "…",
    remainingBytes: null,   // what is left, when the storage tracks it
    durationMs: 260
  },
  creditsUsed: 1
}
```

The object learns from it: `file.sourceId`, `file.location` and `file.metadata` are set, which is what makes [update()](/ar/sdk/ref/files/update) possible straight afterwards.

## يُنتقِشُ أين يذهب {#target}

```javascript
// The account's default
await file.save();

// A storage you named
const all = await api.storage.list();
const archive = all.find((s) => s.label === "Archive");
await file.save({ storageId: archive.id });
```

| نوع | احفظ إلى هذا |
| --- | --- |
| :: قاعدة بيانات | نعم - تُحفظ حسابات الحسابات |
| نظام الملفاتSer | رُفض: storage_is_local |
| ميكرو ميكرو ميكرو ميكرو ميكرو | رُفض: storage_is_local |

يأتي الرفض **ثالثاً** الحساب هو من ليس فشل إلى إعادة المحاولة هذه الأجهزة حفظ ملفات و طريقة كتابتها من جهاز.

## الائتمان، وعندما يعود {#cost}

- أحد الحسابات تؤخذ قبل الكتابة
- إذا كان هذا الخط يُكتب ثمّ إلى **مُرْفِض في** و تحصل على `502 storage_failed`.
- فالرفض الذي يحدث أولا - لا يوجد تخزين، ولا مخزن محلي، ولا محتوى - لا يكلف شيئا على الإطلاق.

## يجري العمل على مستند بعد أن تحصل على جوابك {#indexing}

عندما يكون التصنيف هو: `document`، النص هو مُفهَرس للبحث الدلالي بمجرد إرسال الرد. لذا فإن الملف يمكن قراءته فوراً و إيجاده في كل مرحلة على حدة بعد لحظة.

> **لا تبحث عن شيء حفظته للتو، في نفس نفس نفس نفس**
>
> ( ١ بطرس ٣ : ١ ) فَهُوَ يَحْتَاجُ إِلَى ٱلْمَحَبَّةِ بِٱلْمَسِيحِيِّينَ ٱلْمُتَوَقِّعِينَ .

## الأخطاء {#errors}

| الرمز | الحالة | متى | ) |
| --- | --- | --- | --- |
| — (يُطلَق محليًا) | — | غير معرف أو لاغي. | لم تــ |
| missing_file_content | 400 | لم يصل إلى الخادم ملف أو محتوى نص. | لم تــ |
| storage_not_found | 404 | الحساب ليس لديه تخزين على الإطلاق. | لم تــ |
| storage_is_local | 400 | الهدف هو حاسوب أو هاتف | لم تــ |
| not_enough_credits | 402 | الرصيد لن يغطيه | لم تــ |
| storage_failed | 502 | الـ كتابة مُنتَهِل. | مبعثــــر |

```javascript
import { IaGenifyError } from "iagenify";

try {
  await file.save({ storageId });
} catch (err) {
  if (err instanceof IaGenifyError && err.code === "storage_is_local") {
    // That storage is a device. Write it there instead.
    const device = await api.storage.get({ id: storageId });
    await device.files.write("report.md", file.content);
  } else throw err;
}
```

- [file.update()](/ar/sdk/ref/files/update): تغيير المحتوى بعد ذلك.
- [محارف محلية](/ar/sdk/ref/local-files): الكتابة إلى ملفات الآلية بدلاً من ذلك
- [الأرصدة](/ar/sdk/concepts/credits): كيف يتم أخذ الإنفاق و إعادته

```python
# 1. To the account — the usual one
file.save()

# 2. To the account AND to this machine, in one call
file.save(path="./out/report.md")

# 3. To this machine only — no request, no credit
file.save(path="./out/report.md", local_only=True)
# {"ok": True, "status": "saved_local", "path": "./out/report.md"}
```

```python
{
    "ok": True,
    "status": "saved",
    "sourceId": "…",            # how you find it again
    "name": "report.md",
    "category": "document",
    "mimeType": "text/markdown",
    "location": "object",       # the kind of storage it landed in
    "metadata": {
        "size": 2048,
        "storageId": "…",
        "remainingBytes": None,   # what is left, when the storage tracks it
        "durationMs": 260,
    },
    "creditsUsed": 1,
}
```

```python
# The account's default
file.save()

# A storage you named
archive = next(s for s in api.storage.list() if s.label == "Archive")
file.save(storage_id=archive.id)
```

```python
from iagenify import IaGenifyError

try:
    file.save(storage_id=storage_id)
except IaGenifyError as error:
    if error.code != "storage_is_local":
        raise
    # That storage is a device. Write it there instead.
    device = api.storage.get(id=storage_id)
    device.files.write("report.md", file.content)
```
